K Closest Stars from a Data Stream
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The detail that matters in this Google question from September 2026 is the memory line: process the stream in one pass and keep only O(min(k, n)) rows. That rules out sorting all 200000 stars and slicing. It's a bounded max-heap problem wearing a streaming costume. If you've got an OA invite, expect this shape. The tie-break on starId is where people lose points. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the pattern below should get you most of the way.
The problem
You receive a finite stream of stars in arrival order. Each row stars[i] = [starId, distance] contains a unique integer identifier and that star's nonnegative distance. Return up to k rows with the smallest distances. Order the returned rows by increasing distance; when distances are equal, order them by increasing starId. If k == 0, return an empty matrix. If k is greater than the number of stars, return every row in the required order. Process the input in one pass while retaining only O(min(k, stars.length)) candidate rows before producing the ordered result. Function kClosestStars(stars: int[][], k: int) → int[][] Examples Example 1 stars = [[101,50],[102,20],[103,20],[104,80]] k = 2 return = [[102,20],[103,20]] Stars 102 and 103 have the two smallest distances. Their equal distances are ordered by increasing starId. Example 2 stars = [[7,9],[3,1]] k = 5 return = [[3,1],[7,9]] Because k exceeds the stream length, both stars are returned in increasing distance order. Constraints 0 <= stars.length <= 200000. Every row of stars has exactly two integers: [starId, distance]. All starId values are distinct signed 32-bit integers. 0 <= distance <= 10^9. 0 <= k <= 250000.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Keep a max-heap of size k, ordered by (distance, starId) with the largest pair on top. For each incoming star, push it. If the heap grows past k, pop the top, which is the worst candidate. After the pass, drain the heap and sort ascending by distance, then starId. The common pitfall is comparing distance only. Two stars at the same distance need starId as the secondary key, both in the heap eviction and the final order. Otherwise you evict the wrong one on ties and fail Example 1 style cases. Handle k == 0 up front and return an empty matrix. When k exceeds the stream length, the heap never evicts and you just sort everything. Complexity is O(n log k) time and O(k) space. In a language without a max-heap, negate the keys. StealthCoder is the hedge for the live OA if the comparator logic slips under pressure.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill K Closest Stars from a Data Stream 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as k closest points to origin. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
K Closest Stars from a Data Stream FAQ
What's the trick in the Google K Closest Stars question?+
Use a max-heap capped at size k, keyed on (distance, starId). Evict the largest pair whenever the size exceeds k. That satisfies the one-pass, O(min(k, n)) memory rule. Sorting everything violates the stated constraint even if it passes small tests.
How do I handle ties on distance?+
Compare distance first, then starId, in both the heap ordering and the final sort. Since starIds are distinct, the order is total. In Example 1, stars 102 and 103 both sit at distance 20, and 102 comes first because its id is smaller.
What edge cases should I test?+
Test k == 0 (return an empty matrix), k larger than stars.length (return everything sorted), and an empty stars array. Also test many equal distances, and distances up to 10^9 so you don't overflow with a bad key encoding. Starids can be negative signed 32-bit ints.
How hard is this really?+
It's medium. The idea is standard top-k with a heap. The difficulty is the two-key comparator and keeping heap logic correct when you invert it for a max-heap. If you've written top-k before, it's about 15 lines.
How do I prepare in 48 hours?+
Write the bounded heap solution from scratch twice in your language. Know how to build a max-heap there, whether by negation or a custom comparator. Then run Example 1, Example 2, and k == 0 by hand. Skip broad review and focus on top-k and tie-breaking.