BasicsCentral TendencyGoogleMetaJane Street
Problem
Implement sample_mode(x) returning the most frequent integer in a non-empty list x. If multiple values share the highest frequency, break ties by returning the smallest value.
Why It Matters: Mode is the standard summary statistic for discrete/categorical features and majority-vote ensemble classifiers.
Function Signature
from collections import Counter
def sample_mode(x: list[int]) -> int:
Sample Input & Output
Example 1: Unique Most Frequent Value
Input:
x = [3, 1, 2, 3, 4, 3]
Expected Output:
3
Explanation: 3 appears 3 times, more than any other value.
Example 2: Frequency Tie-Break
Input:
x = [5, 2, 5, 2, 9]
Expected Output:
2
Explanation: Both 2 and 5 appear twice; the smaller value 2 wins the tie-break.
Constraints & Edge Cases
- Return the most frequent value (smallest on tie).
- Break frequency ties deterministically by selecting the smallest numeric value.
- Return a Python
int.
Constraints & Evaluation Criteria
- Return the most frequent value (smallest on tie).
Hints
Python 3⌘/Ctrl+Enter to Run · ⌘/Ctrl+Shift+Enter to Submit
Loading Code Editor...
+1 Hidden Submit Case
Input / Scenario
[4, 2, 4, 3, 4, 2]
Expected Output / Criteria
4