Amazon.com

  www.amazon.com
Work in HR? Unlock Free Profile

Amazon.com Software Development Engineer Intern Interview Question

I interviewed in Seattle, WA and was asked:
"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: data structures, software engineering
Add Tags [?]
Answer

Part of a Software Development Engineer Intern Interview Review - one of 4,771 Amazon.com Interview Reviews

Answers & Comments

0
of 0
votes

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

To comment on this question, Sign In with Facebook or Sign Up

Tags are like keywords that help categorize interview questions that have something in common.