Elements Occurring More Than n/3 Times
Reported by candidates from Mygate's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Mygate reportedly put this one in front of candidates in January 2025, and it's a classic dressed up in a tidy wrapper. Strip the wording and it's a frequency problem: find values appearing more than n/3 times, return them sorted. At most two values can ever qualify, and that's the whole insight. A hash map solves it fast, but the follow-up pressure is usually constant space. If you blank on the voting trick during the live OA, StealthCoder sits invisibly on your screen as a safety net and hands you the solution.
The problem
Given an integer array nums of length n, return every distinct value whose frequency is strictly greater than n / 3. Return qualifying values in increasing numerical order. Frequency counts occurrences of the whole integer value, rather than its individual decimal digits. Return an empty array if no value qualifies. Function majorityOverThird(nums: int[]) → int[] Examples Example 1 nums = [3,2,3] return = [3] The value 3 occurs twice, strictly more than 3 / 3 = 1. Example 2 nums = [1,2,1,2,1,2,3] return = [1,2] Both 1 and 2 occur three times, more than 7 / 3. Example 3 nums = [1,2,3,4] return = [] No frequency exceeds 4 / 3. Constraints 0 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9 For integer frequencies, the threshold is equivalent to frequency > floor(n / 3).
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that no more than two distinct values can each exceed n/3, since three of them would need more than n elements. That unlocks Boyer-Moore voting with two candidates and two counters. Pass one: match a candidate, else fill an empty slot, else decrement both counters. Pass two: count the real frequencies of both candidates and keep those where count > floor(n/3). The second pass is the one people skip, and it's the common pitfall, because the survivors of pass one aren't guaranteed to qualify. Also sort the result before returning, since the output must be increasing. Handle n = 0 by returning an empty array. The hash map version is O(n) time and O(n) space, and it's perfectly acceptable if the OA only checks correctness. If you freeze mid-assessment, StealthCoder can surface the two-candidate voting code so you can verify it against the three examples.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Elements Occurring More Than n/3 Times cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as majority element ii. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Mygate's OA.
Mygate reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Elements Occurring More Than n/3 Times FAQ
What's the trick to Elements Occurring More Than n/3 Times?+
At most two values can appear more than n/3 times. That means you only track two candidates. Use Boyer-Moore with two counters, then verify both candidates with a second counting pass. If you don't need constant space, a hash map counting everything works just as well.
Is a hash map good enough for the Mygate OA?+
Usually yes. Count frequencies, keep values above floor(n/3), sort the result. It's O(n) time plus a tiny sort. Only switch to voting if the prompt demands O(1) extra space. The constraints here allow 10^5 elements, so a map is comfortably fast.
Why do I need a second pass in the voting approach?+
The first pass only gives you candidates, not confirmed answers. For input like [1,2,3,4], the leftover candidates don't actually exceed n/3. Recount each candidate's real frequency and compare to floor(n/3) before adding it to the result.
What edge cases break this problem?+
Empty array returns an empty array. Small n matters, since with n = 1 or 2 the threshold floor(n/3) is 0 or so, and any present value qualifies. Also remember the output must be sorted ascending, and negative values up to -10^9 are valid.
How do I prepare for this in 48 hours?+
Write both versions once from scratch: the hash map and the two-candidate voting. Run the three given examples by hand. Practice the second verification pass and the final sort. That covers nearly every variant of majority-element questions an OA might throw at you.