Replay a key-value log

easyStorage~15 min · 7 tests

Many databases never update data in place on disk. They append each change to a log, and rebuild their state after a restart by reading the log from the top. Write the reader.

Write replay(ops):

  • ops is a list of operations, oldest first. Each one is a list:
    • ["SET", key, value] sets key to value.
    • ["DEL", key] removes key. Deleting a missing key is fine.
    • ["CLEAR"] removes every key.
  • Return the final state as a dict.
  • Any other operation name raises ValueError.
>>> replay([["SET", "a", 1], ["SET", "b", 2], ["DEL", "a"], ["SET", "b", 3]])
{'b': 3}
Learn the ideaRead the post →
solution.pyPython 3.14 · Pyodide

Press ⌘ ↵ or Run to check the visible tests. Submit runs the hidden ones too. Everything runs in your browser.

⌘ ↵ to run