Interview Questions

int IsAnagram(char* str1, char* str2)

Software QA/Tests Interview Questions from Microsoft

(Continued from previous question...)

int IsAnagram(char* str1, char* str2)

int IsAnagram(char* str1, char* str2)

maybe an answer:

i) Add the letters of str1 to a hash table. Key being alphabet and value being the frequency.
ii)Keep track of length of the string, say temp.
iii) For each letter in str2, lookup the hash table:
a) Decrement the value if key is found. Decrement the temp by 1.
If temp is zero finally, anagram found.
If key is found, but value was zero then its not an anagram.
O(n) for n lookups.

maybe an answer2:

1) a) Sort the 2 arrays b)compare each array index one by one.
2) a)add characters of str1 into a hashmap b)store the size of hashmap. c) store characters from str2 into the same hashmap. if size increases its not anagram

(Continued on next question...)

Other Interview Questions