Bazaarvoice Interview Question
Software Engineer / DevelopersHeres the proper question:
Write a method to decide if two strings are anagrams or not?
Here's a definition of anagram from wikipedia:
An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; e.g., orchestra = carthorse, A decimal point = I'm a dot in place. Someone who creates anagrams is called an anagrammatist.[1] The original word or phrase is known as the subject of the anagram.
if you don't understand 0(n+m) study big O notation before you appear for a tech interview.
hope this is no longer ridiculous.
cheers.
Posting an incomplete question and being condescending at the same time. F***ing hilarious.
btw, there is a difference between 0 and O, if you didn't notice, you repeated that mistake.
I do admit that the question is clearer now.
Hahaha... so this is probably the 100th set of insults I've seen on this site. What do you guys do, vent all your condescending, conceited crap out here so that you don't accidentally do it in your interviews? Bloody hell - forget the technical stuff and work on your personalities.
int[] intArray = new int[128];
String str1 = "abcdfer";
String str2 = "abcdfef";
for (int i = 0; i < str1.length(); i++) {
System.out.println((int) str1.charAt(i));
intArray[(int) str1.charAt(i)]++;
intArray[(int) str2.charAt(i)]--;
}
boolean isAnagram = true;
for (int i = 0; i < 128; i++) {
if (intArray[i] != 0) {
isAnagram = false;
}
}
if (isAnagram) {
System.out.println("Is Anagram");
} else {
System.out.println("Not Anagram");
}
1) sort(s1) == sort(s2) => this is the non per-formant solution.
- Anonymous August 17, 20112) Interviewer did not like how it is solved in the career cup coding interview guide. i.e find unique characters in each string and then count total number of unique characters in each string to be equal.
3) Solve this using a Hashtable data structure. loop over the first string add characters and their frequency in the hashtable. loop over the second string look up hastable and decrement the frequency of characters found in the second string. you have an anagram if frequency of all the characters in hastable is zero. Pretty lame compared to how its done in career cup guide, no need to force the data structure but it is what it is.... an interview.