Reported July 2024
Coalitionbinary search

Time Based Key-Value Store

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

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

Coalition reported this one in July 2024, and the input size is the whole story. Up to 100000 operations means you can't scan every stored entry on each get. It's the classic time-based key-value store: set writes a value at a timestamp, get returns the latest value at or before the query time. The pattern is a hash map of lists plus binary search. If the OA lands in your inbox this week, this is a clean one to lock down. And if you blank mid-assessment, StealthCoder runs invisibly on your desktop and gives you the solution in real time.

The problem

Process set and get operations on a time-based key-value store. A get returns the value written for that key at the greatest timestamp not exceeding the query timestamp, or the empty string when none exists. Return "null" for set operations.

Function
runTimeMap(operations: String[], keys: String[], values: String[], timestamps: int[]) → String[]

Examples
Example 1
operations = ["set","get","get","set","get"]
keys = ["foo","foo","foo","foo","foo"]
values = ["bar","","","bar2",""]
timestamps = [1,1,3,4,4]
return = ["null","bar","bar","null","bar2"]
Each query sees the most recent value for foo at or before its timestamp.

Constraints
All four arrays have equal non-zero length.
Set timestamps for each key are strictly increasing.
There are at most 100000 operations.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: map each key to a list of (timestamp, value) pairs. The constraints say set timestamps for each key are strictly increasing, so each list is already sorted. You never sort anything. On get, binary search for the rightmost timestamp that's less than or equal to the query. If none qualifies, return the empty string. Brute force scans the list per get, which turns into O(n^2) across 100000 operations. Binary search makes each get O(log n). Common pitfalls: returning the wrong format, since set must output the string "null" and not a real null. Off-by-one in the binary search is the other one, so test the case where the query is below the first timestamp. Also remember the values array is empty for gets, so don't read it there. If your mind goes blank on the bisect boundary during the live OA, StealthCoder is the hedge that gets you unstuck quietly.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

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 Coalition's OA.

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

Time Based Key-Value Store FAQ

What's the trick to Time Based Key-Value Store?+

Store a list of (timestamp, value) per key in a hash map. Since set timestamps are strictly increasing per key, the list stays sorted without extra work. Then binary search for the largest timestamp at or below the query. That gives O(log n) per get.

Why can't I just loop through the list on each get?+

With up to 100000 operations, a linear scan per get can degrade to roughly n squared work in total. That risks timing out on larger hidden tests. Binary search keeps each lookup logarithmic and is only a few lines more code.

What should set operations return in this Coalition problem?+

The string "null", not a language-level null. The function returns a String array, so each set contributes the literal text "null" at its index. Missing this is an easy way to fail an otherwise correct solution on every test.

What edge cases should I test before submitting?+

Query timestamp lower than the earliest set for that key, a key that was never set, a query exactly equal to a stored timestamp, and a query far beyond the last one. All of those should return the empty string or the right latest value without crashing.

How do I prepare for this in 48 hours?+

Write the solution from scratch twice. Use a map of lists and a hand-written binary search that finds the rightmost index with timestamp at or below the query. Then run the example by hand. That covers the pattern and the boundary handling the OA will check.

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

OA at Coalition?
Invisible during screen share
Get it