29Find the duplicate files

mediumpython
40:00

29 Find the duplicate files

Anthropic candidates report a file deduplication question in phone screens (2 reports) and in onsite coding rounds (2 reports), and a guide to the company's loop lists it among its coding questions (1 source). One candidate found the base problem easy, dedup by file size and then by a hash of the content, and fumbled what came after it: how to tell I/O-bound work from CPU-bound work, what to do with huge files, what changes with a massive number of files, and how to catch a duplicate at the moment a file arrives (1 report). Another had to run against real files and write a test that created them (1 report). LeetCode's Find Duplicate File in System is on OpenAI's company tag lists, the thirty-day list included (4 sources), and on Anthropic's (1 source). This is practice in that shape, not a leaked question or LeetCode's version.

Set a 40-minute timer. Read one level at a time, and run only that level's tests until they pass: pytest problems/29_duplicate_files -k level1. Earlier levels must keep passing.

The answer, at every level. A list of groups. A group is a list of two or more paths whose files have exactly the same content. Sort the paths inside each group, then sort the groups, so the answer doesn't depend on the order you met the files in. A file with no twin is in no group.

Level 1. The content is in the listing. Write duplicates_from_listings(listings). Each string describes one directory: its path, then one name:content entry for each file in it, all separated by single spaces.

docs notes.txt:milk todo.txt:call-home
docs/old notes.txt:milk draft.txt:hello
tmp scratch.txt:call-home

A file's path is its directory, a slash and its name, so these three listings give [["docs/notes.txt", "docs/old/notes.txt"], ["docs/todo.txt", "tmp/scratch.txt"]]. Contents are compared exactly: Milk and milk are different files. No directory or file name holds a space or a colon. A content holds no space, but it can hold colons (standup.txt:09:30 is a file named standup.txt whose content is 09:30) and it can be empty (a.log:). Empty files are duplicates of each other. A directory with no files is listed as its path alone.

Level 2. The content is on disk. Real files don't arrive with their content attached, and reading is the expensive part. Write duplicates_on_disk(files). Each file is an object with path, size in bytes, and read(), which returns the whole content as bytes. Asking for size costs nothing. Every read() is a pass over the whole file, and the tests count the passes.

  • Sizes first. Two files of different sizes can't be equal, so never read a file whose size no other file shares.
  • Read each of the other files exactly once.
  • Don't keep what you read. Thirteen files of 1 MiB each have to fit in 4 MiB at the peak, which the tests measure with tracemalloc. Keep a digest of each file from hashlib, 32 bytes for SHA-256 whatever the file's size, and let the content go.
  • files can be any iterable, including a generator that can only be walked once.

Level 3. Files too big to read at once. read() now raises MemoryError on a big file, so stop calling it. chunks() returns an iterator over the content as bytes pieces, in order. The pieces can be any length, two copies of the same content can be cut in different places, and an empty file yields no pieces. Every rule of level 2 still holds, with one pass of chunks() in place of each read(). The tests hash two 32 MiB files and fail if your code ever holds more than 8 MiB.

Things to get right: what you compare first (the size is free, the content is not), what you keep for each file (a digest, not its bytes), a digest that doesn't depend on where the content was cut, and an answer in a fixed order.

Be ready to talk through what came next in that report (1 report): how you'd find out whether this job is waiting on the disk or busy on the CPU, what you'd change for a hundred million files, how you'd catch a duplicate as a file is written instead of in a nightly scan, and whether a matching digest proves two files are equal. Know how you'd run it on a real folder too, with os.walk, os.path.getsize and files opened in binary mode, and how you'd test that with a temporary directory (1 report).

solution.py
Run the tests to see which pass.