Skip to curriculum
Jump to the editor

19.10. A map that remembers past values

Medium
Topics
20–35 min

A pricing service, a feature-flag store or a collaborative document often has to answer "what was this value at 14:05?", to explain a past order, replay a bug report, or show an audit trail. A plain Map only knows the present. A map that keeps a time-stamped list of every change per key can answer for any moment.

Write the class HistoryMap in history-map.js. It works like a Map for the present, and also records every set and delete with the time Date.now() returns, so getAt(key, time) can return the value a key held at any past time.

Signature

JAVASCRIPT
class HistoryMap {
  get size()          // how many keys are present now
  set(key, value)     // returns the map
  delete(key)         // true if the key was present, otherwise false
  has(key)            // true or false, now
  get(key)            // the present value, or undefined
  getAt(key, time)    // the value at that time, or undefined
  history(key)        // [{ time, value, deleted }, ...], oldest first
}
  • time is in the same milliseconds as Date.now().
  • In history, a set is { time, value, deleted: false } and a delete is { time, value: undefined, deleted: true }.

Requirements

  1. get, has and size describe the present, as in a Map; set returns the map. A key set to undefined is present.
  2. Keys can be any value: 1 and "1" are different keys, and objects and NaN work.
  3. getAt(key, time) returns the value of the latest change made at or before time: the change made exactly at time counts. Before a key's first change it returns undefined.
  4. delete(key) removes the key from the present and returns true; the past is kept, so getAt at an earlier time still returns the old value, and from the delete on it returns undefined.
  5. A delete of a key that is not present records nothing and returns false. A key can be set again after a delete.
  6. When several changes have the same time, the last one wins.
  7. Every set is recorded, even one that keeps the same value.
  8. history(key) returns every change of the key, oldest first, as new objects in a new array (changing them does not change the map); an unknown key gives [].
  9. Values are stored as they are, not copied.
  10. If Date.now() returns a time earlier than the key's last change (the system clock moved back), the change is recorded at the time of that last change, so the history stays in order.
  11. getAt stays correct for a history of tens of thousands of changes; it should not have to scan the whole history.

Examples

JAVASCRIPT
// Date.now() is 1000 here
const prices = new HistoryMap();
prices.set("apple", 10);     // at 1000
// ... 500 ms later
prices.set("apple", 12);     // at 1500

prices.get("apple");          // 12
prices.getAt("apple", 1200);  // 10
prices.getAt("apple", 1500);  // 12: the change at exactly 1500 counts
prices.getAt("apple", 999);   // undefined: before the first change
JAVASCRIPT
const flags = new HistoryMap();
flags.set("beta", true);     // at 2000
flags.delete("beta");        // at 2300: true
flags.has("beta");           // false
flags.getAt("beta", 2100);   // true: the past is kept
flags.history("beta");
// [{ time: 2000, value: true, deleted: false },
//  { time: 2300, value: undefined, deleted: true }]
JAVASCRIPT
const map = new HistoryMap();
map.set("a", 1);
map.set("a", 2);             // in the same millisecond
map.getAt("a", Date.now());  // 2: the last change wins

Environment

  • Plain JavaScript in a browser page; the tests create instances of the global class HistoryMap from history-map.js.
  • Read the time with Date.now(). The tests replace it with a virtual clock.
  • Removing old history, keys() and iteration are not part of this problem.

Hints

  1. The changes of one key are added in time order. What does a sorted list let you do faster than a scan?
  2. When three changes share one time, which of them should a search for that time land on?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…