← Google Interview Insights

Google·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Google SWE coding round, one question but it had real depth to it. Basically a mashup of two problems I'd seen before, which made me overconfident at first.

Questions Asked (1)

Q1

Design a time-based key-value store that supports set(key, value, timestamp) and getClosest(key, timestamp), where getClosest returns the value whose stored timestamp is nearest to the query (not just the floor). Walk through a baseline solution and discuss how you'd improve it.

Algorithms & Data StructuresSystem DesignTechnical Trade-offs
Author's notes

I recognized the two problems it was pulling from pretty fast and jumped straight to the sorted list plus binary search approach.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements: getClosest returns the value with the timestamp nearest to the query, which could be before or after. Propose a baseline solution using a hash map from key to a list of (timestamp, value) pairs, with binary search to find the closest timestamp. Then discuss improvements such as using a balanced BST or sorted list for efficient insertion and querying, and consider trade-offs between time and space complexity.

Pro tip: Explicitly discuss how you would handle ties (e.g., when two timestamps are equally distant) and whether the store should be thread-safe, as these details often matter in production systems.

1. Clarify Requirements

Ask questions to confirm assumptions: Should getClosest return the nearest timestamp even if it's after the query? What about ties? Are timestamps unique per key? Is concurrency a concern?

2. Baseline Solution

Propose a simple approach: store each key's history as a list of (timestamp, value) pairs, keeping it sorted by timestamp. For set, append and sort or insert in order; for getClosest, use binary search to find the insertion point and compare adjacent timestamps.

3. Analyze Complexity

State the time complexity: O(log n) for getClosest with binary search, but O(n) for set if inserting into a list. Space is O(n) for all stored pairs.

4. Propose Improvements

Suggest using a balanced binary search tree (e.g., TreeMap in Java) or a skip list to achieve O(log n) for both set and getClosest. Alternatively, if timestamps are monotonically increasing, a simple append works, but that's not guaranteed.

5. Discuss Trade-offs

Compare approaches: sorted list with binary search is simple but insertion is costly; balanced BST offers better insertion but higher overhead. Mention potential optimizations like bucketing or caching if access patterns are known.

Key Points to Mention

  • Binary search to find the closest timestamp by comparing the predecessor and successor.
  • Handling ties: define a consistent rule (e.g., prefer the earlier timestamp).
  • Time complexity: O(log n) for getClosest, O(n) for set in baseline; O(log n) for both with balanced BST.
  • Space complexity: O(n) where n is total number of set operations.
  • Concurrency: consider thread-safety with locks or concurrent data structures.
  • Edge cases: empty store, key not found, query timestamp outside range.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.