Amazon Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
0
of 0 vote

If external storage(like a hash table with the value as the index) is allowed, then we only need to traverse the string once, whenver one value is seen, just increment the counter associated with that value in the table.
After the traversal, look at the table and output if odd occurrences are seen.

- ultraviolet January 31, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can do it in one pass with constant space. No extra space if you can destroy the array.

- Anonymous February 17, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how?

- ND April 17, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int total=0;
for(int i=0;i<strlen(str);++i)
{
  total=str[i]-total;
}
return total;

- Sridhar March 19, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

very good solution

- burdell July 23, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

oops, does not work for integer array 1,1,2,4,3,3,2,5,5

- burdell August 25, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is possible in one pass without external storage and has a complexity of O(n)

char find_odd_character(char* str)
{
  int i=1, cnt=1;
  char c;
  c=str[0];
  while(str[i]!='\0')
  {
    if(c!=str[i])
    {
       if(cnt%2!=0)
         break;
       else
       {
         c=str[i];
         cnt=1;
       }
    }  
    i++;
  }
  return c;
}

- Fishing April 22, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry! missed some lines

char find_odd_character(char* str)
{
int i=1, cnt=1;
char c;
c=str[0];
while(str[i]!='\0')
{
if(c!=str[i])
{
if(cnt%2!=0)
break;
else
{
c=str[i];
cnt=1;
}
}
else
cnt++;
i++;
}
return c;
}

- Fishing April 22, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just realized that the example above in a little misleading. My code assumed that the characters are sorted. In this case to get a complexity of O(n) we would need some extra storage.

Create an array of size 256 with all elements initialized to 0. Scan the string(str) and increment the appropriate position(str[i]-'a') in the array for each character. Then scan the array and the entry with an odd number is the character that occurs odd number of times.

- Fishing April 22, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good solution.....

- lk August 03, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But why do we require array[256]? isnt array[26] not enough??

can you plz take a alphabet and show i.

- cjs December 04, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is the hash table solution

- sunbow.xs@hotmail.com October 18, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is the hash table solution

- sunbow.xs@hotmail.com October 18, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char oddchar(char ch[])
{
char x = 0;
int i = 0;
while(ch[i])
{
x = x ^ ch[i];
i++;
}
return x;
}

- Ravi August 01, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Ravi

Ur code doesnt run in all the cases. What if there are two characters occuring odd # of times in src string. It doesnt run.

- stuart August 02, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

apologies!!

i dint see the phrase "one value" in problem statement.

Runs perfectly fine ..!!! :)

- stuart August 02, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

no need to apologise , u r right if there r two characters occuring odd number of times , the result returned is not correct.

- lk August 03, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If its a char array, just do a XOR...

char t;
t = a[0];
for(int i=1; i < n; ++i)
  t = t ^ a[i];

- k May 30, 2008 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More