Majority Element II
MediumArrays & Hashingboyer-moore voting
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Given an integer array nums of length n, return every value that appears **strictly more than** n / 3 times (that is, more than floor(n / 3) times).
At most two values can qualify. Return them in **ascending order**; return [] if none qualify.
Solve it in linear time using O(1) extra space.
Examples
Input: nums = [3,2,3]
Output: [3]
n = 3, and 3 appears twice, which is more than 1.
Input: nums = [1]
Output: [1]
The only value appears once, which is more than 0.
Input: nums = [1,2]
Output: [1,2]
Both values appear once, which is more than floor(2/3) = 0.
Constraints
- 1 <= nums.length <= 5 * 10^4
- -10^9 <= nums[i] <= 10^9
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.