← Questions
K-th largest value over the last N seconds of a stream
0
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in SpaceXAI's onsite applied coding round, last asked Jan 2026. It tests heaps, sliding window and streaming.
Two parts. First, return the k-th largest element of an array. Then values arrive one at a time with timestamps, and queries ask for the k-th largest among the values seen in the last N seconds. Design a structure that handles inserts, expiry of old values and queries efficiently.
Part 1: k-th largest in an array
-
kth_largest(nums: list[int], k: int) -> int: the k-th largest value counting duplicates (k = 1is the maximum).1 <= k <= len(nums).
kth_largest([3, 2, 1, 5, 6, 4], 2) -> 5
kth_largest([3, 2, 3, 1, 2, 4, 5, 5, 6], 4) -> 4
Part 2: sliding time window
Unless you agree on a different interface with the interviewer, assume:
WindowKth(window: int, k: int)-
add(ts: int, value: int) -> None: records a value at timets. Timestamps never decrease. -
query(ts: int) -> int | None: the k-th largest among values with timestamp in(ts - window, ts], orNoneif fewer thankvalues are in the window.tsis at least the last added timestamp.
w = WindowKth(window=10, k=2)
w.add(1, 5); w.add(3, 9); w.add(4, 7)
w.query(5) -> 7 # window holds 5, 9, 7
w.add(12, 1)
w.query(12) -> 7 # 5 (ts 1) expired; 9, 7, 1
w.query(13) -> 1 # 9 (ts 3) expired; 7, 1
w.query(14) -> None # only 1 is left
Constraints
- Up to 10^5
addandquerycalls. - Values are integers; ask whether they fall in a small bounded range.
Clarifying questions
- Do duplicates count separately toward k?
- Is
kfixed at construction or given per query? - Can timestamps arrive out of order, and do queries come at the current time only?
- Are values bounded (for example 0 to 1000), which allows counting buckets?
- Is the window boundary inclusive or exclusive?
What a strong answer covers
- Part 1: a size-k min-heap in O(n log k), or quickselect in expected O(n); their trade-offs.
- Part 2: a FIFO queue of
(ts, value)for expiry plus an order-statistics structure (sorted container, two heaps with lazy deletion, or a Fenwick tree over value ranks) for O(log n) insert, delete and k-th. - With bounded values, a count array or bucket counts give O(1) updates and fast k-th lookup.
- Expiry done lazily on each call, and each value removed exactly once.
- Tests at the window edge, with duplicates, fewer than k values and an empty window.
Follow-ups
-
kchanges per query. - The median of the window instead of a fixed k.
- Out-of-order timestamps within a bounded lateness.
- Memory is capped and the window holds millions of values: what approximation would you accept?
Where it's asked
- SpaceXAI · Onsite applied coding · Last asked Jan 2026Two or three rounds, in person (Palo Alto for many roles)
What it tests
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding
