The Crowd Splitter
An easy Python interview practice problem on DataDriven. Write the Python and run it against test cases, with instant feedback.
- Domain
- Python
- Difficulty
- easy
- Seniority
- L4
- Asked in
- Alcon
Problem
Given a list of integers where one value appears strictly more than half the time, return its value. The median equals that dominant value.
The middle holds even with a dominant outlier.
Example 1
Input:
nums = [1,2,2,2,3]
Output:
2
Example 2
Input:
nums = [7,7,7,7,1,2,3]
Output:
7
Worked solution and explanation
Why this problem exists in real interviews
This tests whether a candidate recognizes that a majority element is always the median of the list. It probes algorithmic insight: if one value dominates more than half the positions, it must occupy the center after sorting.
Break down the requirements
Step 1: Sort the list
With a majority element guaranteed, the median position is always occupied by that element.
Step 2: Return the middle element
Index len(nums) // 2 gives the median. The majority element is guaranteed to be there.
The solution
def find_median(nums: list) -> int:
sorted_nums = sorted(nums)
median = sorted_nums[len(sorted_nums) // 2]
return medianCommon follow-up questions
- How would you find the median without sorting? (Tests quickselect algorithm or Boyer-Moore for the majority case.)
- What if no majority element exists? (Tests that the median-equals-majority shortcut no longer applies.)
- What is the space complexity of quickselect? (Tests O(1) for in-place partition vs O(n) for the sorted-copy approach.)