Find Most Frequent Values
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates proficiency in analyzing element frequency distributions, choosing appropriate data structures, and optimizing top-k selection with attention to time and space complexity.
Part 1: Find Most Frequent Values Using Counting and Sorting
Constraints
- 1 <= len(nums) <= 100000
- 1 <= k <= number of distinct values in nums
- -1000000000 <= nums[i] <= 1000000000
- The input is guaranteed to contain at least k distinct values.
Examples
Input: ([1, 1, 1, 2, 2, 3], 2)
Expected Output: [1, 2]
Explanation: 1 appears 3 times and 2 appears 2 times, so they are the two most frequent values.
Input: ([5], 1)
Expected Output: [5]
Explanation: Edge case: the array has only one value.
Hints
- First count how many times each distinct value appears.
- Sort the value-frequency pairs by negative frequency, then by the value itself.
Part 2: Find Most Frequent Values Using a Heap
Constraints
- 1 <= len(nums) <= 100000
- 1 <= k <= number of distinct values in nums
- -1000000000 <= nums[i] <= 1000000000
- The input is guaranteed to contain at least k distinct values.
- The intended ranking approach should use a heap and avoid sorting all distinct values.
Examples
Input: ([1, 1, 1, 2, 2, 3], 2)
Expected Output: [1, 2]
Explanation: 1 and 2 have the two highest frequencies.
Input: ([7], 1)
Expected Output: [7]
Explanation: Edge case: only one distinct value exists.
Hints
- You still need a hash map to count frequencies before using the heap.
- Maintain a min-heap of at most k candidates so the least desirable candidate can be removed when the heap grows too large.