PracHub
QuestionsCoachesLearningGuidesInterview Prep

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.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

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.

Quick Answer: 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.

Input: (['set', 'set', 'set', 'get', 'get', 'get', 'get'], ['love', 'love', 'hate', 'love', 'love', 'hate', 'hate'], ['high', 'low', 'hot', '', '', '', ''], [10, 20, 5, 15, 25, 5, 4])

Expected Output: ['high', 'low', 'hot', '']

Explanation: This checks multiple keys. 'love' returns 'high' at time 15 and 'low' at time 25. 'hate' returns 'hot' at time 5 and empty at time 4.

Input: (['set', 'set', 'get', 'get', 'get'], ['k', 'k', 'k', 'k', 'k'], ['v1', 'v2', '', '', ''], [2, 8, 1, 2, 7])

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

Explanation: This checks boundary behavior around exact and in-between timestamps. Before time 2 there is no value, at time 2 the answer is 'v1', and at time 7 the most recent earlier value is still 'v1'.

Input: ([], [], [], [])

Expected Output: []

Explanation: With no operations, there are no get results to return.

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.
Last updated: Jun 7, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • In-Memory URL Shortener: Encode and Decode - Microsoft (medium)
  • Minimum Moves on a Grid with k-Cell Jumps - Microsoft (medium)
  • Return Top K Open Businesses - Microsoft (hard)
  • Implement Memory Allocation and In-Memory Records - Microsoft (medium)
  • Compute the Product of an Array Except Self - Microsoft (medium)