15% off all plans & roadmaps Use code
See plansLeetCode 981 · Solution & editorial
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.
Time Based Key-Value Store is a Design Patterns problem, in the Design (General/Specific) subpattern. The pattern covers tries, LRU/LFU cache, and data structure design
Design and implement data structures: LRU cache, LFU cache, HashMap, and specialized structures.
The reference solution runs in O(1) time and O(K + Q) space.
Time Based Key-Value Store has been reported in interviews at Oracle, ByteDance, Uber, Apple, Lyft, Sumologic, Google, Atlassian.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Time Based Key-Value Store is a Design Patterns problem, in the Design (General/Specific) subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.