Reported February 2026
Googlebinary search

Closest-Timestamp Key-Value Queries

Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Google OA. Under 2s to a working solution.
Founder's read

This Google OA from February 2026 looks like a wordy design question, but it's a hash map plus binary search. Group entries by key, then answer each query by finding the nearest timestamp in that key's sorted list. With up to 200000 entries and 200000 queries, a linear scan per query will time out, so the shape of the solution matters. The tie rule and the missing-key case are where people lose points. If you blank on the lower-bound step during the live OA, StealthCoder runs invisibly as a safety net and gives you the working code while you keep typing.

The problem

You receive timestamped key-value entries through parallel arrays. Entry i stores entryValues[i] for entryKeys[i] at entryTimes[i]. For each individual key, its timestamps appear in strictly increasing order, although entries for different keys may be interleaved.
For every pair (queryKeys[i], queryTimes[i]), return the value stored for that key at the timestamp closest to the query time. If the closest timestamps on the two sides are equally far away, choose the earlier timestamp. Return the empty string when the key has no stored entry.

Function
closestValues(entryKeys: String[], entryTimes: int[], entryValues: String[], queryKeys: String[], queryTimes: int[]) → String[]

Examples
Example 1
entryKeys = ["a","b","a","a","b"]
entryTimes = [1,2,5,9,8]
entryValues = ["one","bee2","five","nine","bee8"]
queryKeys = ["a","a","b","c"]
queryTimes = [7,3,6,4]
return = ["five","one","bee8",""]
For key a, time 7 ties between 5 and 9, so time 5 wins. Time 3 is closer to 1 than 5. For key b, time 8 is closer than 2. Key c is absent.
Example 2
entryKeys = ["x","x"]
entryTimes = [10,20]
entryValues = ["old","new"]
queryKeys = ["x","x"]
queryTimes = [5,25]
return = ["old","new"]
A query before all entries chooses the first timestamp, and a query after all entries chooses the last timestamp.

Constraints
0 <= entryKeys.length <= 200000
entryKeys.length == entryTimes.length == entryValues.length
1 <= queryKeys.length == queryTimes.length <= 200000
Keys and values contain 1 to 40 printable ASCII characters.
0 <= entryTimes[i], queryTimes[i] <= 1000000000.
For each key, entry timestamps are strictly increasing in encounter order.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: build a map from key to two parallel lists, times and values. The input already guarantees timestamps are increasing per key, so no sorting is needed. For each query, look up the key. If it's missing, return the empty string. Otherwise binary search for the first timestamp >= queryTime. That index gives you the right neighbor, and index minus one gives the left neighbor. Clamp at the edges, so a query before everything returns the first entry and one after everything returns the last. Compare the distances. If they're equal, take the left one, the earlier timestamp. The common pitfall is using <= or < the wrong way on the tie, or forgetting that the left neighbor may not exist. Another is scanning linearly per query. Total cost is O((n + q) log n). StealthCoder is the hedge if you freeze on the boundary handling during the live OA.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Closest-Timestamp Key-Value Queries 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 by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Google's OA.

Google reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Closest-Timestamp Key-Value Queries FAQ

What's the actual trick in this Google closest-timestamp problem?+

Group entries by key in a hash map, then binary search each key's timestamp list. The per-key timestamps are already increasing, so you skip sorting. Find the first timestamp at or after the query, compare it with the one before it, and pick the closer one.

How do I handle ties correctly?+

If the left and right distances are equal, return the left value, the earlier timestamp. In code, pick the right neighbor only when its distance is strictly smaller than the left distance. Example 1 checks this: key a at time 7 sits between 5 and 9 and returns five.

What edge cases break most solutions?+

Missing keys must return an empty string. Queries before the first timestamp or after the last one have only one neighbor, so clamp the index. Also watch for entryKeys being empty, which means every query returns an empty string.

Will a brute-force scan pass?+

Probably not. With 200000 entries and 200000 queries, scanning a key's whole list per query can approach 4 x 10^10 operations in the worst case. Binary search per query brings it to roughly (n + q) log n, which is comfortable.

How do I prepare for this in 48 hours?+

Practice writing lower-bound binary search from memory until the boundary cases feel automatic. Then write this solution once end to end with a hash map of lists. Test your code against both examples, including the tie and the missing key, before the OA starts.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Google.

OA at Google?
Invisible during screen share
Get it