Amazon Interview Question
Country: India
yes, I told about using hash map to count frequency and then min-heap. hash map will grow very large in case of large numbers. So we can make use of multiple hashes or B-trees to store the hash map.
yes, I told about using hash map to count frequency and then min-heap. hash map will grow very large in case of large numbers. So we can make use of multiple hashes or B-trees to store the hash map.
How about using tree map. It sorts key values automatically. We can put each number from file as key and number of occurrences of that number as value pair. Finally take top k values.
Could use external merge sort to sort by value. Then walk through sorted array, count frequency of each number and add to a min-heap. If min heap has more than k elements, remove minimum. Overall effort O(n log n) for sorting, O(n * log k) for building up and maintainig k element min heap, so O(n log n).
- Anonymous December 17, 2011