Stage 1 of 4: Same bytes, any name
Background. A disk fills up with copies: the same bytes saved again under another name, in another folder. disk.py models a disk in memory, and your code reaches its files only through a Disk object.
Implement duplicate_sets(disk, root) in dedupe.py.
Look at every file under the directory root (a path such as /data), in its subdirectories at any depth too, and return the files whose contents are equal as a list of groups. A group is a list of the full paths of two or more files with exactly the same bytes. Names and folders decide nothing, and a file whose bytes no other file under root has is in no group.
From disk.py you need:
disk.list_dir(path)returns the entries of a directory, each anEntry(name, kind, size, mtime); herekindis"file"or"dir".disk.open(path)opens a file;handle.read()returns all its bytes. Use the handle in awithblock.- A full path is the directory's path,
/and the entry's name:/data/old/a.txt.
Example
/data/a.txt b"hello"
/data/b.txt b"world"
/data/old/a.txt b"hello"
/data/old/c.txt b"hello"
/data/old/d.txt b"hullo"
duplicate_sets(disk, "/data") returns [["/data/a.txt", "/data/old/a.txt", "/data/old/c.txt"]].
Notes
- The groups may come in any order, and so may the paths inside a group.
- Empty files have equal contents: two or more of them make a group.
- Files outside
rootare not part of the answer. - Every tree here holds files and directories only.
Done when Submit goes green.
Constraints
- Python 3.12 standard library only
- Editable:
dedupe.py - Read-only:
disk.pyand alltests/test_*.pyfiles - The disk is the in-memory
Diskofdisk.py; there is no real filesystem
What comes after stage 1
The next stages open once the tests for the one before them pass. Each raises the bar, the way an interviewer does in the room.
- Stage 2: Open fewer files. Reading is the slow part of a scan. Find the same groups while opening only the files that could have a copy.
- Stage 3: Large files, false matches. Files get large and most of them differ early, and two different files can share a hash. Read less, and never be fooled.
- Stage 4: Links, locks and moving files. A real tree has links that loop, files the scan may not read and files written while it reads them. Finish the scan anyway.
The files you get
- Yours to edit: dedupe.py.
- Read-only: disk.py.
- Tests you can read and run: tests/test_part1.py. Hidden tests run on Submit.
The round: no AI, an assistant that only answers
Anthropic runs this round without AI. Here the closest format is answer-only: the assistant answers your questions and reviews your code when you ask, the way an interviewer would, and never writes a line of it. The interviewer asks about your decisions as you go, and the report states the format. You can switch to the agent format when you press Begin.
Frequently asked, briefly answered
Is Identical Files free?
Stage 1 is free with an account. The other 3 stages need Nyrion Pro.
Which company asks something like this?
Anthropic. It is modelled on Anthropic's coding round, which runs without AI. Based on candidates' reports of Anthropic interviews. Nyrion wrote this version, so the question you get may differ.
Do I bring my own AI tool?
No. The real round has no AI, so this one opens answer-only: an assistant that answers your questions and can't write code. You can switch to an agent that proposes changes when you press Begin.
What does the interviewer ask?
5 questions along the way about your own code, and one at the end about the whole session. A weak answer gets one follow up. The answers go into the report, which ends with a hire or no hire verdict.
Start the problem
Sign in and the problem opens where you left it. New here? An account takes a minute to create, and the first stage of this problem is free.
More like it: every practice problem, Anthropic's interviews, the AI coding interview guide.