1/40
Learn

Find the duplicate files

Anthropic candidates report a file deduplication question in phone screens (2 reports) and in onsite coding rounds (2 reports). One of them found the base problem easy and stumbled on the follow-ups (1 report). Level 1 reads like this:

Each string in listings describes one directory: its path, then one name:content entry for each file in it, all separated by single spaces. Write duplicates_from_listings(listings): the groups of two or more paths whose files have exactly the same content. Sort the paths inside each group, then sort the groups.

For example, listings holds these three strings:

  • "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 the answer is [["docs/notes.txt", "docs/old/notes.txt"], ["docs/todo.txt", "tmp/scratch.txt"]]. Contents compare exactly: Milk and milk are different files. A content can hold colons, like standup.txt:09:30, and it can be empty, like a.log:.

Level 2 takes the content off the disk. Each file has a size that costs nothing and a read() that costs a full pass, and the tests count the passes and the memory. Level 3 makes the files too big to read at once, so the content comes in pieces. Then come the follow-ups: is the job waiting on the disk or busy on the CPU, what changes for a hundred million files, and does a matching digest prove two files are equal?

You don't write it yet. Every level comes down to one question, asked fast: have I seen this before? So the lesson starts with sets and dicts, and builds up to keys for whole files. At the end you solve the whole problem, level by level, in an interview simulation with a 40-minute timer.

Hash maps come first because almost every lesson after this one uses them. The sliding window keeps its counts in a dict, the grid search keeps the cells it has seen in a set, and the database in the coding builds, where small systems grow in levels, is a dict of dicts.