KatInterview

Content-Based Duplicate File Detector

Trees & GraphsModel answer
MediumModel answerTrees & Graphshashingfile-systemtraversal

Last updated

Recursively scan a directory tree, group files by size then content hash, and return every set of two or more paths that hold identical bytes.

Content-Based Duplicate File Detector

You are building a storage-cleanup utility. Given a path to a root directory, write a function that finds every group of files whose contents are byte-for-byte identical and returns those groups.

Function signature

def find_duplicates(root: str) -> list[list[str]]:
    ...

What to return

A list of groups. Each group is a sorted list of absolute (or root-relative) paths. Only groups with at least two members should appear. The outer list should also be sorted. Return an empty list when no duplicates exist.

Requirements

  1. Traverse the directory tree recursively, including all subdirectories.
  2. Use file size as a first filter — files with different sizes cannot be identical.
  3. For files that share a size, use a content hash to identify matches.
  4. Never load an entire file into memory at once; use fixed-size read buffers so the solution works on arbitrarily large files.
  5. If a file cannot be read (permission denied, deleted mid-scan, etc.) skip it and continue — do not crash.
  6. If a file changes while it is being hashed, discard it from results rather than reporting a potentially inconsistent hash.

Example

root/
  a.txt       ("hello")
  b.txt       ("hello")
  sub/
    c.txt     ("hello")
    d.txt     ("world")
find_duplicates("root")
# → [["root/a.txt", "root/b.txt", "root/sub/c.txt"]]

d.txt has no duplicate, so it does not appear.

Constraints

  • The tree can be arbitrarily deep.
  • Files can range from 0 bytes to many gigabytes.
  • The directory may contain symlinks, hard links, FIFOs, and other non-regular entries.
  • Assume a single machine for the base solution; scaling questions are discussed as follow-ups.

Follow-up discussion (no code needed, talk through the ideas)

  • Is this workload I/O-bound or CPU-bound, and how would you find out?
  • How would you handle files that are hundreds of gigabytes?
  • How would you scale to a filesystem with billions of files?
  • Where would you introduce parallelism, and what are the risks?
  • How would you support near-real-time detection as files arrive or change?

Hints

Hint 1

Before reading any bytes, group files by the size reported in their metadata. A stat call costs almost nothing and immediately eliminates files that cannot possibly match anything.

Hint 2

Reading the entire content of every same-size file is wasteful. Hash only the first 64 KB first — files that differ early are eliminated after a tiny read, and only survivors go on to a full hash.

Hint 3

When you open a file for hashing, check its size and modification time before and after reading. If either changed, the hash describes a moving target and should be thrown away, not returned as a result.