Top K With a Deterministically Noisy Comparator (MLE)
Reported by candidates from Mercor's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Mercor reported this one in July 2026, and it looks like a plain top-k until you read the comparator clause. The heap is the data structure everything hinges on, but the twist is that cmp lies 10% of the time and repeats the same lie. So retrying a call buys you nothing. If your OA invite lands in the next day or two, expect the interviewer to care more about the guarantee you state than the code. StealthCoder is the safety net running invisibly during the live OA if you blank on the noisy-comparator framing.
The problem
You are given n numbers and a comparator function cmp(a, b). The comparator is intended to compare two numbers, but it is unreliable. The comparator has a 10% chance of returning the wrong result. The error is deterministic for the same arguments: if cmp(x, y) is wrong once, calling cmp(x, y) again returns the same wrong result. Given an array of numbers and an integer k, design and implement a function that returns the top k largest numbers. List<Integer> topK(int[] nums, int k) Because the comparator can be wrong, your answer should explicitly state what guarantee you are targeting: exact correctness if possible, or a high-confidence result under a probabilistic error model. FastPrep executable note: The original interview discussion focuses on comparator strategy, not output ordering. To make the workspace checker deterministic, return the selected top-k values in descending numeric order. Comparator Contract cmp(a, b) < 0 means a is smaller than b cmp(a, b) > 0 means a is larger than b cmp(a, b) = 0 means a and b are equal Repeating the same comparison does not reduce the error probability, because the mistake is deterministic for the same pair. Important Assumption To Clarify Unless the interviewer states otherwise, assume each unordered pair of values has an independent corrupted comparison result with probability 10%. Once a pair is corrupted, all future calls on that same pair return the same corrupted result. If the comparator can be adversarially wrong, or if no probabilistic model is available, exact top-k correctness cannot be guaranteed in general. Clarifications / Corner Cases k = 0: return an empty list. n = 0: return an empty list. k >= n: return all numbers. Duplicate values may appear. The original prompt did not require sorted output unless the interviewer asks for sorted top-k; this FastPrep version returns descending order for deterministic checking. Calling cmp on the same pair multiple times is not useful for reducing error. The comparator may appear inconsistent because different pairwise results may be corrupted. If the error model is adversarial, exact correctness cannot be guaranteed in general. If errors are random per pair, the algorithm can aim for high-confidence top-k. The algorithm should avoid unnecessary comparator calls. Comparator calls may be very slow. Follow-up A - Minimize Comparator Calls How would you minimize the number of calls to cmp(a, b)? Discuss the tradeoff between doing fewer comparisons and collecting enough independent evidence to handle a noisy comparator. Follow-up B - Slow Comparator Calls Assume each call to cmp(a, b) takes about 5 seconds. How would you minimize actual wall-clock runtime? Which comparisons can be run in parallel? How would you batch independent comparator calls? How would you cache pairwise results to avoid duplicate calls? How would you reduce long sequential dependency chains? How would your design change if you can use async workers or distributed execution? Function topK(nums: int[], k: int) → int[] Examples Example 1 nums = [10, 8, 3, 20, 15] k = 2 return = [20, 15] True top 2: [20, 15]. A standard comparator-based top-k algorithm may return the true top 2, but a wrong comparison near the top-k boundary can cause an incorrect result. Example 2 nums = [5, 1, 4, 3, 2] k = 3 return = [5, 4, 3] True top 3: [5, 4, 3]. If cmp(4, 3) is wrong, calling cmp(4, 3) repeatedly will still return the wrong answer. A robust approach must rely on information from other comparisons, not repeated calls to the same pair. Example 3 nums = [100, 99, 98, 1, 0] k = 3 return = [100, 99, 98] True top 3: [100, 99, 98]. The most important uncertainty is often around the boundary between rank k and rank k + 1. A single corrupted comparison involving boundary candidates may change the returned set. Constraints 0 <= nums.length <= 100,000 0 <= k <= nums.length cmp(a, b) is deterministic for the same ordered arguments. The cost of cmp may dominate all other computation.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The base solution is a min-heap of size k. Push each number, pop the smallest when size exceeds k, then sort the survivors descending. That's O(n log k) comparator calls. The trick is the honest guarantee. Because errors are deterministic per pair, repeating cmp(x, y) adds zero information. To get real evidence you need comparisons against different elements, like a few extra pivots or a voting pass near the rank k boundary, where a single corrupted result can flip the set. State the assumption: independent 10% corruption per unordered pair gives a high-confidence result, not exact. If the errors are adversarial, exact is impossible. The common pitfall is retrying the same pair and calling it a fix. Also cache pair results and handle k = 0, n = 0 and duplicates. StealthCoder is the hedge if you freeze on the follow-ups mid-OA.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Top K With a Deterministically Noisy Comparator (MLE) 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as kth largest element in an array. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Mercor's OA.
Mercor reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Top K With a Deterministically Noisy Comparator (MLE) FAQ
What's the trick in the Mercor noisy comparator top-k problem?+
Repeating cmp on the same pair is useless because the error is deterministic. Use a size-k min-heap for the base solution, then add evidence from different pairs near the boundary. State clearly that you're targeting high confidence, not exact correctness.
Can I guarantee an exact top-k answer?+
Not in general. If the comparator can be adversarially wrong, no algorithm guarantees exactness. Under the stated model of independent 10% corruption per pair, you can only target a high-confidence result. Say this out loud before coding.
How do I minimize comparator calls?+
A size-k heap costs about n log k calls. Cache every pair result so you never call the same pair twice. Fewer calls means less independent evidence, so spend extra comparisons only on elements near the rank k and k+1 boundary.
What changes if each cmp call takes 5 seconds?+
Wall-clock time dominates. Batch independent comparisons and run them in parallel with async workers. Use a tournament-style or partition structure to shorten sequential dependency chains, and keep a shared cache so workers don't duplicate pair calls.
How do I prepare for this in 48 hours?+
Write the size-k min-heap top-k cold, including edge cases: k = 0, n = 0, k >= n, duplicates. Then rehearse a 30-second explanation of why retries fail and what guarantee you target. Return the result in descending order, as this version requires.