Reported November 2023
SpaceXhash table

Simplified Time-Based Key-Value Store

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

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

SpaceX reportedly put a time-based key-value store in front of candidates in November 2023, and the detail that matters is buried in the constraints: set timestamps are strictly increasing. That one line changes how you build it. This is LeetCode's Time Based Key-Value Store dressed up as a batch function, with string-encoded timestamps and a results array for get calls only. It's a hash map plus binary search, and it's very doable if you stay calm. If you blank mid-assessment, StealthCoder runs invisibly as a safety net and hands you the structure. Here's the script before you need it.

The problem

Implement a simplified time-based key-value store by processing a sequence of operations in order.
Each operation is represented as an array of strings:
["set", key, value, timestamp] stores value for key at timestamp.
["get", key, timestamp] asks for the value stored for key at the greatest set timestamp less than or equal to timestamp.
The timestamp in each operation is a base-10 integer encoded as a string. If a get operation has no qualifying stored value, its result is the empty string. Return the results of the get operations in their original order; set operations do not add an entry to the returned array.

Function
timeMapResults(operations: String[][]) → String[]

Examples
Example 1
operations = [["set","foo","bar","1"],["get","foo","1"],["get","foo","3"],["set","foo","bar2","4"],["get","foo","4"],["get","foo","5"]]
return = ["bar","bar","bar2","bar2"]
The first two queries use the value stored at timestamp 1. After the second set, queries at timestamps 4 and 5 use bar2.
Example 2
operations = [["get","missing","7"],["set","x","one","10"],["get","x","9"],["get","x","10"]]
return = ["","","one"]
The missing key and the query before x is first stored both return an empty string. The exact-timestamp query returns one.
Example 3
operations = [["set","alpha","a1","2"],["set","beta","b1","3"],["set","alpha","a2","8"],["get","alpha","7"],["get","beta","100"],["get","alpha","8"]]
return = ["a1","b1","a2"]
Histories are independent by key, and each query uses the latest qualifying timestamp for that key.

Constraints
1 <= operations.length <= 200000
Every operation begins with "set" or "get".
A set operation has exactly four fields; a get operation has exactly three fields.
1 <= key.length, value.length <= 100, and keys and values contain only lowercase English letters and digits.
Every timestamp is a base-10 integer in the range [1, 10000000].
The timestamps of set operations are strictly increasing.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: map each key to a list of (timestamp, value) pairs. Because set timestamps are strictly increasing globally, every per-key list is already sorted, so you just append. For get, binary search for the rightmost timestamp less than or equal to the query. If none qualifies, or the key is missing, push an empty string. Pitfalls are small but real. Parse timestamps from strings to integers before comparing, since string comparison breaks on "10" versus "9". Don't append anything to the output for set operations. Handle a get on a key that was never set, as Example 2 shows. With 200000 operations, a linear scan per get can go quadratic, so use binary search. If you freeze on the bisect boundary, StealthCoder is the hedge on the live OA: it reads the problem and gives you working code while you keep typing like normal.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Simplified Time-Based Key-Value Store 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as time based key value store. If you have time before the OA, drill that.

⏵ The honest play

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

SpaceX 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.

Simplified Time-Based Key-Value Store FAQ

How hard is the SpaceX time-based key-value store problem really?+

Medium at most. It's a hash map of sorted lists plus one binary search. The only real work is the boundary condition on the search and remembering that set operations produce no output. If you've seen the LeetCode version, this is a reskin.

What's the trick to solving it fast?+

Timestamps for set are strictly increasing, so you never sort. Append to a per-key list and binary search on get for the largest timestamp less than or equal to the query. Return an empty string when nothing qualifies or the key doesn't exist.

Do I need a custom binary search or can I use a library one?+

Either works. A library upper-bound call on the timestamp list, then step back one index, gives you the answer. Writing it by hand is fine too. Just make sure you find the rightmost timestamp at or below the query, not the first match.

What edge cases should I test before submitting?+

Get on a missing key, get before the first set for a key, get at an exact timestamp, and get far above the last timestamp. Also test interleaved keys, like Example 3, to confirm histories stay separate. Parse timestamps as integers, not strings.

How do I prepare for this in 48 hours?+

Write the hash map plus binary search version from scratch twice. Then run the three given examples by hand. Practice the bisect boundary until it's automatic. This pattern shows up often in design-style OA questions, so the muscle memory pays off.

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

OA at SpaceX?
Invisible during screen share
Get it