Quick Overview

This question evaluates understanding of time-versioned data structures and efficient temporal lookup within associative mappings, focusing on storing and retrieving multiple values per key across timestamps.

Build a time-based key-value store

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem Design a data structure that stores multiple values for the same key at different timestamps, and can retrieve the most recent value as of a given timestamp. Implement a class `TimeMap` with the following operations: - `set(key, value, timestamp)` - Stores the string `value` for string `key` at integer `timestamp`. - `get(key, timestamp) -> string` - Returns the value associated with `key` at the **largest timestamp `t` such that `t <= timestamp`**. - If there is no such timestamp for the key, return an empty string `""`. ### Notes / Constraints (typical) - `key` and `value` are strings. - `timestamp` is a positive integer. - `set` calls for the same key may be given in increasing timestamp order (state your assumption; if not guaranteed, handle it). - Target complexity: about **O(log m)** per `get`, where `m` is the number of stored timestamps for that key.

Overview: This question evaluates understanding of time-versioned data structures and efficient temporal lookup within associative mappings, focusing on storing and retrieving multiple values per key across timestamps.

You are given four parallel arrays that describe operations on an initially empty time-based key-value store. For each index i: - If ops[i] == "set", store values[i] for key keys[i] at timestamp timestamps[i]. - If ops[i] == "get", query the value for key keys[i] at timestamp timestamps[i]. Return the value stored at the largest timestamp t such that t <= timestamps[i]. If no such timestamp exists for that key, return an empty string "". Process operations in the given order. Return the answers for all "get" operations in the order they appear. Assumption: for any single key, its "set" operations are given in strictly increasing timestamp order.

Constraints

  • 0 <= len(ops) == len(keys) == len(values) == len(timestamps) <= 200000
  • ops[i] is either "set" or "get"
  • 1 <= timestamps[i] <= 10^9
  • key and value are strings
  • For any fixed key, timestamps for its set operations are strictly increasing

Examples

Input: (['set', 'get', 'get', 'set', 'get', 'get'], ['foo', 'foo', 'foo', 'foo', 'foo', 'foo'], ['bar', '', '', 'bar2', '', ''], [1, 1, 3, 4, 4, 5])

Expected Output: ['bar', 'bar', 'bar2', 'bar2']

Explanation: The key 'foo' has values 'bar' at time 1 and 'bar2' at time 4. Each get returns the most recent value at or before the requested timestamp.

Input: (['get', 'set', 'get', 'get'], ['a', 'a', 'a', 'b'], ['', 'x', '', ''], [1, 5, 4, 2])

Expected Output: ['', '', '']

Explanation: The first get asks for a key that does not exist yet. After setting 'a' at time 5, querying at time 4 still returns an empty string because no timestamp <= 4 exists. Key 'b' was never set.

Hints

  1. Store all timestamps and values for each key together so you can search only within that key's history.
  2. Because timestamps for each key are already sorted, use binary search to find the rightmost timestamp less than or equal to the query time.

Build a Time-Based Key-Value Store

mediumMicrosoftSoftware Engineer

You are given four parallel arrays that describe operations on an initially empty time-based key-value store.

For each index i:

  • If ops[i] == "set", store values[i] for key keys[i] at timestamp timestamps[i].
  • If ops[i] == "get", query the value for key keys[i] at timestamp timestamps[i]. Return the value stored at the largest timestamp t such that t <= timestamps[i]. If no such timestamp exists for that key, return an empty string "".

Process operations in the given order. Return the answers for all "get" operations in the order they appear.

Assumption: for any single key, its "set" operations are given in strictly increasing timestamp order.

Examples

Example 1
Input
(['set', 'get', 'get', 'set', 'get', 'get'], ['foo', 'foo', 'foo', 'foo', 'foo', 'foo'], ['bar', '', '', 'bar2', '', ''], [1, 1, 3, 4, 4, 5])
Output
['bar', 'bar', 'bar2', 'bar2']
Notes
The key 'foo' has values 'bar' at time 1 and 'bar2' at time 4. Each get returns the most recent value at or before the requested timestamp.
Example 2
Input
(['get', 'set', 'get', 'get'], ['a', 'a', 'a', 'b'], ['', 'x', '', ''], [1, 5, 4, 2])
Output
['', '', '']
Notes
The first get asks for a key that does not exist yet. After setting 'a' at time 5, querying at time 4 still returns an empty string because no timestamp <= 4 exists. Key 'b' was never set.

Constraints

  • •0 <= len(ops) == len(keys) == len(values) == len(timestamps) <= 200000
  • •ops[i] is either "set" or "get"
  • •1 <= timestamps[i] <= 10^9
  • •key and value are strings
  • •For any fixed key, timestamps for its set operations are strictly increasing

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def solution(ops: list[str], keys: list[str], values: list[str], timestamps: list[int]) -> list[str]:
Sample call
solution(['set', 'get', 'get', 'set', 'get', 'get'], ['foo', 'foo', 'foo', 'foo', 'foo', 'foo'], ['bar', '', '', 'bar2', '', ''], [1, 1, 3, 4, 4, 5])
Language
Case 1
Input
(['set', 'get', 'get', 'set', 'get', 'get'], ['foo', 'foo', 'foo', 'foo', 'foo', 'foo'], ['bar', '', '', 'bar2', '', ''], [1, 1, 3, 4, 4, 5])
Expected
['bar', 'bar', 'bar2', 'bar2']
Case 2
Input
(['get', 'set', 'get', 'get'], ['a', 'a', 'a', 'b'], ['', 'x', '', ''], [1, 5, 4, 2])
Expected
['', '', '']

Browse More Questions