progress

~/problems / Stateful stores / In-memory file system

Cloud storage with versions and trash

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

medium assessment 4 levels ~90 min Anthropic

Level 1 Files and sizes

Build VersionedStorage, a cloud store of files. A file has a name, which is a path-like string such as "/docs/plan.txt", and a size, a positive integer. Names are plain string keys: there are no folder objects, and a name is a file only if it was added.

  • add_file(name, size) -> bool: add a new file. Return False, changing nothing, if a file with that name already exists.
  • overwrite_file(name, size) -> int | None: give an existing file a new size and return its previous size. Return None if there's no such file.
  • get_file_size(name) -> int | None: the file's size, or None if there's no such file.
  • delete_file(name) -> int | None: remove the file and return its size, or None if there's no such file.
  • list_files(prefix) -> list[str]: every file whose name starts with prefix, written as "<name>(<size>)" and sorted by name in string order. An empty prefix matches every file.
s = VersionedStorage()
s.add_file("/docs/a.txt", 10)        # True
s.add_file("/docs/a.txt", 99)        # False: the name is taken
s.add_file("/docs/b.txt", 5)         # True
s.add_file("/img/c.png", 70)         # True
s.overwrite_file("/docs/a.txt", 12)  # 10: the previous size
s.overwrite_file("/nope", 1)         # None
s.get_file_size("/docs/a.txt")       # 12
s.list_files("/docs/")               # ["/docs/a.txt(12)", "/docs/b.txt(5)"]
s.delete_file("/img/c.png")          # 70
s.list_files("")                     # ["/docs/a.txt(12)", "/docs/b.txt(5)"]

Constraints: up to 10^5 calls, sizes up to 10^9, names up to 30 characters.

Later levels keep every version of a file, add a trash, and add users with backups. Each level unlocks when the previous one passes.

⭐ Bonus: a speed test with 20,000 files and 20,000 prefix queries earns a star if a query doesn't look at every file.

Level 2 unlocks when level 1 passes.

Level 3 unlocks when level 2 passes.

Level 4 unlocks when level 3 passes.

Topic: In-memory file system. Path hierarchy as nested dicts; quotas.

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