Latest-Timestamp Key-Value Store
Problem statement
Implement a key-value store that chooses values by their supplied timestamps, not by arrival order. The four aligned arrays describe a finite sequence of operations:
operations[i] = 0: performset(timestamps[i], keys[i], values[i]).operations[i] = 1: performget(keys[i]). Ignoretimestamps[i]andvalues[i]for this operation.
A get returns the value from a preceding set for that key whose timestamp is greatest. If several preceding sets have that greatest timestamp, use the one that occurs latest in the operation sequence. If the key has never been set, return the empty string.
Examples
Example 1
operations = [0,0,1,0,1,0,1]keys = ["a","a","a","a","a","a","a"]values = ["newer","older","","tie","","newest",""]timestamps = [10,3,0,10,99,12,1]return = ["newer","tie","newest"]The write at timestamp 3 does not replace timestamp 10. The later timestamp-10 write wins the tie. Timestamp 12 then becomes the greatest. Get timestamps are ignored.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 1 more worked example, explained
- Guided hints and editorial
- Run your code on real test cases
$99 billed yearly — or $19 month-to-month. Cancel anytime.