Flipkart Interview Question for Software Engineer / Developers






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

My soln assumes that two words are anagrams if they are separated from each other by given delimiters like space etc and they contain same letters.

e.g. I saw a cat at the tac.

Here I can say that <cat, tac> is a anagram pair.

Algorithm:
Tokenize the words
As you are tokenizing, put the words in the hashtable as <word sorted by characters, actual word> e.g.if you find cat put <key, value> as <act, cat>
If you already find that word in hash table while putting in, you have found the anagram.
Time Complexity - O(len of str) assuming words are of constant size (lets say max size = 25).

Space complexity O(n) for the hash table.

Does anyone have better answer for this?

- coder December 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

O(len of str)?
Sorting the words would also take time.
At best its O(nlgn)

- abc August 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorting of word would itself take O(nlogn) where n is the length of the string.
Instead we can take 1st 26 prime number and allocate it to a-z. Now for every word we compute the product of the characters, this will be unique as we have chosen prime numbers.
Now for all the words Map it to HashMap<Integer,Word> if the key is there we have found one pair, else we insert the element into the map.

- Ravi Ranjan March 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Good job coder, I liked your solution, way simple

- Lahori December 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

anon and coder solutions are superb.

Trie can be used in this case.

- master February 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Before inserting a word in a trie ,sort the word by its characters.
Do the same to determine , a given word has its anagram in the trie

- swapna July 19, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If you sotr before inserting, how do you retrieve them back.

- Manoj August 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think for anagrams best solution is to use Prime numbers assign A-2, B-3, C-5 etc., and put them in hash then CAB is 5*3*2 = 30, use this as the key and value can be list of Strings.

Do this for all words and finally iterate over the hash to print the anagrams.
This deals away the sorting burden.

- Manoj K March 01, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thanks manoj this 1 seems a better solution

- anshulzunke September 16, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

The solution works. But the downside is that if we have very large sentence or text, the cost of multiplying i.e. tokenising the entire text would be very high.

For log words, i.e. of 10 characters, there is a potential chance of arithmetic overflow and a high cost of multiplication.

I'd suggest keep it simple by sum of ascii values and the no. of characters in a word. There would be collisions but would be very minimal.

tac would have the same weightage as tbb, act etc.

A trie would suffice. No hashtable required.

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

we can also put the indexes of the words in the hash to reduce space usage...

- pb March 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can also put the indexes of the words in the hash to reduce space usage...

- pb March 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Staying with the assumption from coder, you can use a trie (prefix tree) instead of a hash. When determining if a word is an anagram, you would check to see if the word's reverse is in the trie. The advantages of using a trie:

1) No need for hashing algorithm
2) No collisions
3) Less storage (not entirely sure on this one)

- anon December 07, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

how is this going to work?i don't think it works. "reverse of word?" what are you checking here?

- Anonymous December 09, 2009 | 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