Interview Question

Interview Seattle, WA

Implement a stack that supports push, pop and mode(the one

  from statistics) operation. Gave an O(log n ) push and pop and O(1) mode operation.Another good question was implement atoi function. I assumed it as base 10 but was asked to support from binary to any base numbers(even base 50).
Tags:
Answer

Interview Answer

2 Answers

0

Solution to atoi function().It supports only base 10 operations. int atoi(char *str) { int is_negative = 0; int int_val = 0; /* Skip non digit characters excluding '-' */ while (!isdigit(*str) && (*str != '-')) { str++; } /* Number is negative */ if (*str == '-') { is_negative = 1; str++; } /* Compute the int_val for contiguous digits */ while (isdigit(*str)) { int_val = (int_val*10) + (*str - '0')%10; str++; } return ((is_negative) ? (int_val * -1): int_val); }

Love Bisaria on Mar 12, 2014
0

how you get a O(log n) of push and pop in a stack?

Anonymous on Mar 26, 2015

Add Answers or Comments

To comment on this, Sign In or Sign Up.