KatInterview

Versioned Lookup Store with Inclusive Range Event Tally

Data Structure DesignAuto-graded · 20 test cases
MediumAuto-gradedData Structure Designbinary-searchhash-mapdesign

Last updated

Simulate a batch of commands on a per-key timestamped value history and an event log, using binary search for point-in-time lookups and inclusive range counts.

You are asked to simulate a small system that holds two independent time-aware components, driven by a list of commands.

Components

1. Versioned store. Every key keeps a history of values, each stamped with the time at which it was written.

  • set(key, value, timestamp) writes value for key at timestamp. If key already has a value written at exactly that timestamp, the new value replaces the old one. Writes may arrive in any timestamp order.
  • get(key, timestamp) returns the value of key with the greatest write time that is less than or equal to timestamp. If the key was never written, or every write to it happened after timestamp, return the empty string "".

2. Event tally. A single global log of event times, shared by all commands (not per key).

  • record(timestamp) adds one event at timestamp. Several events may share the same timestamp, and each one is counted separately. Events may arrive in any order.
  • count(start, end) returns how many recorded events have a timestamp t with start <= t <= end. Both ends are inclusive. If start > end, the answer is 0.

The store and the tally do not affect each other.

Task

Implement a function that receives two parallel lists:

  • operations: command names, each one of "set", "get", "record", "count".
  • arguments: for the command at the same index, its argument list:
    • "set" -> [key, value, timestamp]
    • "get" -> [key, timestamp]
    • "record" -> [timestamp]
    • "count" -> [start, end]

Process the commands in order and return a list of the same length as operations. For set and record the entry is None; for get it is the string found; for count it is the integer count. Queries only see commands that appear earlier in the list.

Constraints

  • operations and arguments have the same length.
  • Keys and values are strings; timestamps and range bounds are integers.
  • Timestamps need not be increasing, and may repeat.
  • Only the four command names above appear.
  • Aim for logarithmic-time lookups per query (plus the cost of keeping data ordered), rather than scanning all history for every query.

Examples

Example 1

Input: run_time_commands(['set', 'set', 'get', 'get', 'get'], [['color', 'red', 1], ['color', 'blue', 4], ['color', 3], ['color', 4], ['color', 0]])

Output: [None, None, 'red', 'blue', '']

Example 2

Input: run_time_commands(['record', 'record', 'record', 'count', 'count'], [[2], [2], [5], [2, 5], [3, 4]])

Output: [None, None, None, 3, 0]

Hints

Hint 1

For each key, keep its (timestamp, value) pairs ordered by timestamp, so a lookup can binary search instead of scanning.

Hint 2

For get, you want the last entry whose timestamp is <= the query time. Find the insertion point to the right of the query time and step back by one; if that index is negative, return an empty string.

Hint 3

Keep the event timestamps in one sorted list. An inclusive count is the number of items <= end minus the number of items < start, and you should return 0 when start > end.