Top K Frequent Closest Points
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The whole question hinges on one data structure: a heap with a custom comparator, sitting on top of a frequency map. This Google OA, reported in February 2026, hands you up to 200000 points and asks for the k most frequent distinct ones, with ties broken by distance, then x, then y. It reads like two easy problems stapled together. The trap is the four-level ordering and getting it wrong under pressure. StealthCoder is the safety net if your mind goes blank mid-assessment, but the pattern is simple enough to walk in with.
The problem
You are given an array of two-dimensional integer points. Equal coordinate pairs represent repeated observations of the same distinct point. Return the k distinct points with the highest frequencies, ordered by: higher frequency first; smaller squared Euclidean distance x*x + y*y from the origin; smaller x coordinate; smaller y coordinate. Return each selected point once. Function topKFrequentClosest(points: int[][], k: int) → int[][] Examples Example 1 points = [[1,1],[1,1],[2,0],[2,0],[2,0],[0,3]] k = 2 return = [[2,0],[1,1]] [2,0] occurs three times and [1,1] occurs twice, so frequency fixes their order. Example 2 points = [[3,0],[0,2],[-2,0],[3,0],[0,2],[-2,0]] k = 3 return = [[-2,0],[0,2],[3,0]] All three points occur twice. The two distance-four points tie on distance, so smaller x puts [-2,0] first; [3,0] is farther away. Constraints 1 <= points.length <= 200000 Every row has exactly two integers x and y with -100000 <= x, y <= 100000. 1 <= k <= the number of distinct coordinate pairs. Use signed 64-bit arithmetic for squared distances.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Step one: count occurrences of each point in a hash map keyed by the (x, y) pair. That gives you distinct points with frequencies. Step two: order them by the tuple (-frequency, x*x + y*y, x, y). You can sort all distinct points with that key, which is O(n log n) and perfectly fine for 200000. Or keep a heap of size k with the inverse comparator for O(n log k). Sorting is the lower-risk choice. The pitfalls are all small. Squared distance can reach 2 * 10^10, so use 64-bit. Don't mix up the direction of each tie-break. Return each point once, not once per occurrence. If you freeze on the comparator during the live OA, StealthCoder can give you the working solution on screen while you stay calm and keep typing.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Top K Frequent Closest Points 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
You've seen the question.
Make sure you actually pass Google's OA.
Google 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 Frequent Closest Points FAQ
What's the trick in Top K Frequent Closest Points?+
Count frequencies with a hash map, then sort or heap by a four-part key: higher frequency, smaller squared distance, smaller x, smaller y. No square roots needed. The whole problem is getting that comparator right and not overthinking the structure.
Should I use a heap or just sort?+
Sorting the distinct points is simpler and fast enough for 200000 inputs. A size-k heap gives O(n log k) but needs a reversed comparator, which is easier to get wrong. If you're nervous, sort. Switch to a heap only if you're confident.
Why does the problem mention signed 64-bit arithmetic?+
Coordinates go up to 100000 in absolute value, so x*x + y*y can hit 2 * 10^10. That overflows a 32-bit int. In Java or C++ use long. Python doesn't care. It's a quiet bug that fails only on large hidden tests.
How do I handle the tie-breaking correctly?+
Build a key tuple: (-freq, x*x + y*y, x, y) and sort ascending. Negating the frequency makes higher counts come first. Check Example 2: three points tie at frequency two, and the two distance-four points are split by smaller x, so [-2,0] beats [0,2].
How do I prepare for this in 48 hours?+
Write the solution once from scratch: frequency map, then a custom sort key. Test it on both examples by hand. Then do one more problem that mixes counting with a multi-key sort. The pattern is small, so repetition on comparators matters more than breadth.