progress

~/problems / Stateful stores / In-memory database

Records of fields in nested dicts

On a phone? Coding is easier on a laptop: email this problem to yourself . Meanwhile: quiz this topic or fight a boss.

easy basics ~10 min

Every in-memory-database assessment starts from this model: a record named by a string key, holding fields that map to string values. Build RecordStore with:

  • set(key, field, value): write the field, creating the record if needed.
  • get(key, field) -> str | None: the value, or None if missing. Never raises, never creates anything.
  • delete(key, field) -> bool: remove the field (True), or False if it wasn't there. A record with no fields left is gone.
  • fields(key) -> list[str]: the record's field names, sorted; [] if missing.
  • count() -> int: how many records exist.
s = RecordStore()
s.set("user1", "name", "Ann")
s.set("user1", "city", "Oslo")
s.get("user1", "name")      # "Ann"
s.get("user2", "name")      # None
s.fields("user1")           # ["city", "name"]
s.delete("user1", "name")   # True
s.delete("user1", "name")   # False
s.delete("user1", "city")   # True
s.count()                   # 0: user1 has no fields left
Show hint

a dict of dicts (key -> {field: value}), read with .get(...); delete the inner dict when it empties.

Topic: In-memory database. CRUD with fields, range scans, TTL expiry, time-travel reads.

Read the visual guide
0:00
Ctrl ' run · Ctrl ↵ submit
esc