EvalFrame Technical Mock Interviews & Coding Practice

Time Based Key-Value Store

Medium · Binary Search · Algorithm problem · all problems

Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key's value at a certain timestamp. Implement the `TimeMap` class: * `set(key: string, value: string, timestamp: number): void` Stores the key with value at given timestamp. * `get(key: string, timestamp: number): string` Returns a value such that `set` was called with `timestamp_prev <= timestamp`. If no such value exists, returns `""`. **Examples:** ```typescript const timeMap = new TimeMap(); timeMap.set("foo", "bar", 1); timeMap.get("foo", 1); // return "bar" timeMap.get("foo", 3); // return "bar" timeMap.set("foo", "bar2", 4); timeMap.get("foo", 4); // return "bar2" timeMap.get("foo", 5); // return "bar2" ```

Solve It in the BrowserPractice Explaining It Out Loud

Optimal Approach

  • Time complexity: set: O(1), get: O(log n)
  • Space complexity: O(n)

Set appends in O(1). Get binary searches monotonically increasing timestamps in O(log n).

Follow-Up Questions Interviewers Ask About Time Based Key-Value Store

  • Why does binary search work on the timestamp array?
    Problem guarantees timestamps in set calls are strictly increasing, so the list for each key is pre-sorted.

Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.