Return the Most Frequent Integer with a Stable Tie-Breaker
Company: Bytedance
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: Implement a frequency-based selection over a large integer array: return the most common value, using the numerically smallest value to break ties. Preserve the input and return the value itself rather than its count or position.
Constraints
- 1 <= len(values) <= 200,000
- -10^9 <= values[i] <= 10^9
- Do not mutate values.
Examples
Input: ([1],)
Expected Output: 1
Explanation: Checks unique maxima and numeric tie-breaking, including negatives.
Input: ([4, 1, 4, 2, 2],)
Expected Output: 2
Explanation: Checks unique maxima and numeric tie-breaking, including negatives.
Hints
- Count each value with a hash map.
- Compare candidates first by larger count and then by smaller numeric value.