Amazon Interview Question for Software Engineer in Tests






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

Also tell the number of occurrences of each word

- neo April 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. String Tokenize and use hashmap and insert all the records and increment the value on collision [N]
2. return the top value words [Nlog N]

Time Complexity : NlogN

- Tarun Phaugat April 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

how to return top values word from hash map? Do v need to iterate through it ?

- neo April 12, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

yes you need to.....as you said you want count of all the words as well...

- Anonymous April 12, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Still how will you find three most frequent words by iterating

- Jim February 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

once you have all the frequencies in the map, since the map is unordered, we need to sort the map based on values. Use collections.sort passing the entry set and comparator as arguments to it. comparator's compare to function can sort the entries based on the values(frequencies).. Thus your map is sorted and you can print the values of top K elements from the map.

Map<String,Integer> sortByValue(Map<string,Integer> m) {
List<Map.Entry<String,Integer>> list = new LinkedList<Map.Entry<String,Integer>>(map.entryset());

Collections.sort(list, new Comparator<Map.Entry<String,Integer>>(){ public int compare(Map.Entry<String,Integer> m1, Map.Entry<String,Integer> m2){

return (m2.getValue()).CompareTo(m1.getValue());
}
});

Map<String,Integer> result = new LinkedHashMap<String,Integer>();

for(Map.Entry<String,Integer> entry : list) {
result.put(entry.getKey(),entry.getValue());
}
}

- Boyer Moor February 09, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In a very large file find the 3 most frequent words
Sort the words in the file, now just walk through the list. This is O(nlogn). Better algorithm will be to hash each word, and then walk through the hash and report three words with max occurences.

Also tell the number of occurrences of each word
Above idea will suffice.

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

Use Heap; with keys being the frequency of occurrence.

- Raj April 13, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Nice

- Messi April 20, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Bala.

Not really. Think about it.

- Anonymous April 21, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if know the frequency, why create a heap .. while knowing the frequency keep track of max.

- Anonymous May 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well it's a "very large" file. The idea is that there isn't as much memory. So holding the complete data structure in memory is not possible.

Just like in External merge sort, you sort chunks of file and write all the sorted chunks to a temporary file. Now in the memory, create k chunks(k = number of chunks in file), although the chunk size in memory is not as big as it is in the file. This is fine since we'd load the remaining data in the memory chunk from the file chunk as we reach the end of memory chunk. Create a min-heap using the top elements (min elements) of each memory chunk. Now just keep Extracting the min element and filling in the heap. Since you are getting all the elements in sorted order, it's easy to keep track of 3 most used keys so far.

- drekhi May 14, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

you could use an approx. alrgorithm.

- top K May 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Map reduce should work

- Anonymous May 26, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is similar to twitter's trending topic

- ASB October 18, 2010 | 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