Skip to the code
cf.
+25 XP

#160 Settings History Store

medium

A collaborative editor records every change to a document setting, so it can show what the settings were at any moment. Build a class SettingsHistory:

  • set(key, value, time) records that setting key became value at time. For each key, time only grows from one set call to the next.
  • get(key, time) returns the value of key at time: the value from the latest set for that key whose time is at most time. If there is none, return the empty string "".

set returns nothing. Make get fast even when a key has many changes.

Each example lists the calls in order, then each call's arguments. The first call creates the object with new SettingsHistory(…); the output lists what each call returns (null for the constructor and for methods that return nothing).

Example 1

Input: ["SettingsHistory","set","get","get","set","get","get"]
[[],["theme","light",1],["theme",1],["theme",0],["theme","dark",4],["theme",3],["theme",5]]
Output: [null,null,"light","",null,"light","dark"]

At time 3 the latest change was light (time 1). At time 5 it is dark (time 4). Nothing was set before time 1.

Example 2

Input: ["SettingsHistory","get","set","get"]
[[],["font",10],["font","serif",10],["zoom",10]]
Output: [null,"",null,""]

Asking before any change gives "", and zoom was never set.

Constraints

  • Up to 100000 calls in total.
  • 1 <= time <= 10^9
  • Keys and values are non-empty strings.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).