progress

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

Camera card: find by extension, delete folders

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

easy ~15 min

A camera saves files in nested folders like /DCIM/2025_06_01/IMG_0001.jpg. Model its memory card.

Implement CameraCard() with:

  • save(path): store a file, creating missing parent folders. Saving an existing file changes nothing.
  • find(folder, ext) -> list[str]: sorted full paths of every file at any depth under folder whose name ends with "." + ext (case-sensitive; "jpg" alone doesn't match). A missing folder, or a file path, gives [].
  • remove(path) -> int: delete the file or whole folder at path and return how many files went. Its parent stays, even if empty. Nothing there: return 0. remove("/") empties the card.
card = CameraCard()
card.save("/DCIM/day1/a.jpg")
card.save("/DCIM/day1/a.xmp")
card.save("/DCIM/day2/b.jpg")
card.save("/c.jpg")
card.find("/", "jpg")          # ["/DCIM/day1/a.jpg", "/DCIM/day2/b.jpg", "/c.jpg"]
card.find("/DCIM/day1", "xmp") # ["/DCIM/day1/a.xmp"]
card.remove("/DCIM/day1")      # 2
card.find("/DCIM", "jpg")      # ["/DCIM/day2/b.jpg"]
card.remove("/DCIM/day1")      # 0   (already gone)

Paths are absolute with no trailing slash (except "/"); files and folders never collide.

Show hint

nested dicts (name → dict for a folder, None for a file). find is a DFS carrying the path so far; remove walks to the parent and deletes one key, counting files in what it removes.

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

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