KatInterview

Hardening a Duplicate-File Finder Against Real Filesystems

System DesignModel answer
HardModel answerSystem Designfile-deduplicationfilesystemshashing

Last updated

Design a duplicate-file cleanup tool that survives live, messy filesystems: handle errors, races, loops, scale and hash choices, and make deletions provably safe.

You are building a command-line tool that finds identical files across large directory trees and can optionally reclaim space by removing duplicates. The basic idea is simple: bucket files by size, then compare content hashes. Your task is to discuss what makes this hard when it runs on a real, busy machine.

Walk through the problems such a tool is likely to hit and give a concrete mitigation for each. Cover at least these areas:

  • Permission failures and I/O errors
  • Files being modified, replaced, or removed while the scan runs (time-of-check vs. time-of-use)
  • Path length and filename encoding limits
  • Symlink cycles, hard links, bind mounts and special files
  • Memory and disk throughput bottlenecks at tens of millions of files
  • Choice of hash function and the risk of collisions
  • Partial hashing versus full hashing, and when each is enough
  • Parallelism and throttling so the tool doesn't starve other workloads
  • Incremental rescans and crash-safe checkpointing
  • Safe cleanup actions (verification before deletion, hard links, reflinks, dry runs)

State your assumptions about scale and environment first, and explain which trade-offs you favor and why.

Hints

Hint 1

Organize your answer as a pipeline (walk, filter by size, cheap partial hash, full hash, byte comparison, action) and attach failure modes to each stage rather than listing problems at random.

Hint 2

Decide on the correctness bar up front: a missed duplicate costs a little disk space, but a false match followed by deletion destroys data. Let that asymmetry drive every decision, including whether to trust a hash alone.

Hint 3

Think about what can change between the moment you inspect a file and the moment you act on it. Record identity and timestamps, re-check them before acting, and consider scanning a filesystem snapshot for busy trees.

Hint 4

Remember that two paths pointing to the same inode are not duplicates of each other, and that opening special files such as FIFOs can hang the scan.