Amazon Interview Question for Software Engineer / Developers






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

With no additional storage, you can sort and compare O(nlgn)
With additional storage, its O(n)

- Kishore March 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, seems perfect and simple solution.

- Roger March 17, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned int charsum(char *str)
{
  unsigned int x = 0;
  unsigned int i;
  for(i = 0; i < strlen(str); i++)
    x += str[i];
  return x;
}

int anagram(char *str1, char *str2)
{
  return charsum(str1) == charsum(str2);
}

int main(int argc, char**argv)
{
  if (argc != 3)
  {
    printf("usage: %s str1 str2\n");
    exit(1);
  }
  int x = anagram(argv[1], argv[2]);
  printf("%s and %s are%s anagrams.\n", argv[1], argv[2], x ? "" : " NOT");
}

$ ./anagram abcdefg bdagfce
abcdefg and bdagfce are anagrams.

$ ./anagram foo bar
foo and bar are NOT anagrams.

- leeym March 07, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If the input is "ad" and "bc"; it fails.

- Vaibhavs March 07, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int findAna(char *s1, char*s2){

int sum = 0;

while(*s1 != '\0')
sum ^= *s1++;
while(*s2 != '\0')
sum ^= *s2++;

return sum;

}

void main (){

int i;

i = findAna("abcd","bac");

printf("%d",i); // i = 0 ? => anagrams .... else not anagrams :)
}

- RaZ March 10, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

p.s. sry fr da imba space inbetween :P

- RaZ March 10, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

First compare the lengths of the two strings s1 and s2. If they are unequal, return false. If they are equal, then put s2 in a Hash Map. Iterate through s1 character by character. In each iteration, query the character of s1 with the Hash Map. If a collision occurs, increment a "collision counter" variable that keeps track of the total number of collisions at the end of the iterations. At the end, compare the length of the string with the value of the counter. If they are equal, that means there have been as many collisions as there are characters in the two strings, which consequently implies that the two strings s1 and s2 are anagrams of each other.

- Owl March 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It can be done in O(n) runtime and O(1) space. See question id 8248783.

- john1732 March 26, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

@john1732,
I don't see a solution with O(n) runtime and O(1) space under that id. If I missed/overlooked the solution, can you please paste the relevant section ?

- yadudoc April 21, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why cant we just Xor all the characters in both the arrays and check if the result is 0

- pup May 12, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

XOR solution will not work .. consider a example of two String "AAAA" and "BBBB"

- Anonymous July 17, 2011 | Flag


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