Rank the Top Two Competition Teams
Reported by candidates from ZipRecruiter's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The ZipRecruiter OA reported in September 2023 looks like a ranking story, but it reduces to one thing: a custom sort with three keys. Compute points as 3 * wins + draws, compute goal difference as scored minus conceded, then order by points descending, difference descending, index ascending. Return the first two indices. If you've got an invite and 48 hours, this is a warm-up, not a wall. The only way to lose is a sloppy comparator or overthinking it. StealthCoder sits invisibly on your screen during the live OA as a safety net if you blank on the tuple sort syntax, but you probably won't need it.
The problem
Four aligned arrays describe each team. Team i earns 3 * wins[i] + draws[i] points and has difference scored[i] - conceded[i]. Rank teams by points descending, then difference descending, then index ascending. Return the indices of the top two teams. Function topTwoTeams(wins: int[], draws: int[], scored: int[], conceded: int[]) → int[] Examples Example 1 wins = [3,2,1] draws = [0,2,5] scored = [5,5,5] conceded = [1,1,1] return = [0,1] The primary point totals determine the top two. Example 2 wins = [2,2,1] draws = [0,0,3] scored = [10,7,9] conceded = [4,4,4] return = [0,2] Equal points are ordered by larger scored-minus-conceded difference. Constraints 2 <= wins.length <= 100000 All four arrays have equal length and contain values from 0 through 100000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that you don't need a full sort. Build a key per team, then either sort all n teams in O(n log n) or scan once, tracking the best two in O(n). Both pass at 100000 teams. The clean version is a sort with the key (-points, -diff, index). Negating the first two fields gives descending order while index stays ascending, so ties resolve for free. The common pitfall is comparing only points and forgetting the difference tiebreak, which fails Example 2 where teams 0 and 1 tie on 6 points. Another is returning points or sorted values instead of original indices. Keep the index attached to each record before sorting. Values stay small, so overflow isn't a concern, and difference can be negative, so don't assume it's nonnegative. If your mind goes blank on the comparator mid-assessment, StealthCoder can surface the tuple-key sort so you just type it out.
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 Rank the Top Two Competition Teams 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
You've seen the question.
Make sure you actually pass ZipRecruiter's OA.
ZipRecruiter 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.
Rank the Top Two Competition Teams FAQ
How hard is the ZipRecruiter top two teams question really?+
Easy. It's a multi-key sort with a simple formula for points and difference. If you know how to sort tuples or write a comparator in your language, you can finish it in minutes. The risk is carelessness, not difficulty.
What's the trick to the tiebreakers?+
Build a key of (-points, -difference, index) and sort ascending. Negating the first two fields flips them to descending, and the index stays ascending. That handles all three ranking rules in one pass with no custom comparison logic.
Do I need to sort the whole array?+
No. A single pass tracking the best and second-best team works in O(n). Sorting is O(n log n), which is fine for 100000 teams. Pick whichever you can write without bugs. Sorting is usually the safer choice.
What edge cases should I test?+
Test ties on points where difference decides, ties on both where index decides, and negative differences when conceded exceeds scored. Also confirm you return original indices, not sorted positions. Minimum length is 2, so there are always two teams to return.
How do I prepare for this in 48 hours?+
Practice sorting by multiple keys in your chosen language, including descending order and index tiebreaks. Write this problem once from scratch, then run both examples. That's enough. Spend the remaining time on other sorting and ranking patterns.