Top K With a Deterministically Noisy Comparator (MLE)
Updated
Problem statement
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.
Output order: The main question is comparator strategy, not output ordering. To make the answer 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 equalRepeating the same comparison does not reduce the error probability, because the mistake is deterministic for the same pair.
Important Assumption To Clarify
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.
- Return the top k in descending order.
- Calling
cmpon 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 = 2return = [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 = 3return = [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 = 3return = [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,0000 <= k <= nums.lengthcmp(a, b)is deterministic for the same ordered arguments.- The cost of
cmpmay dominate all other computation.
More Mercor interview questions
- Merge Sort Report with a Noisy ComparatorONSITE INTERVIEW · Seen Sep 2026
- Minimum Friend API CallsONSITE INTERVIEW · Seen Sep 2026
- Park Train Cars by PreferenceONSITE INTERVIEW · Seen Sep 2026
- Currency Conversion RateONSITE INTERVIEW · Seen Sep 2026
- Expected Fake-Name CollisionsPHONE SCREEN · Seen Oct 2026
- Length of a Document Reached Through a Random TokenPHONE SCREEN · Seen Oct 2026
- Precision of Stacked PII Detector FlagsPHONE SCREEN · Seen Oct 2026
- Sort by Checking PermutationsPHONE SCREEN · Seen Oct 2026
- Coffee Rank ProbabilityPHONE SCREEN · Seen Oct 2026
- CSV Row SerializerPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Top K Largest Values from a StreamThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
- Fixed-K Kth Largest StreamAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Fixed-K Kth Largest StreamGoogle · ONSITE INTERVIEWGoogle interview questions
- Find Median from Data StreamAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Thread-Safe LRU Cache with TTLLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
- Employee Free TimeIDFC Bank · ONSITE INTERVIEWIDFC Bank interview questions
- Minimum Refueling StopsUber · ONSITE INTERVIEWUber interview questions
- Find Median from Data StreamBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Running Stream MediansCitadel · ONSITE INTERVIEWCitadel interview questions
- Running Median of a StreamPeopleStrong · PHONE SCREEN · ONSITE INTERVIEWPeopleStrong interview questions