Group files that share a content hash to find duplicates in O(n) with a dict of lists. Covers hashing large files in chunks with hashlib. Tested live.
The problem
hashes maps a file name to its content hash. Return a list of groups of files that share a hash — only groups with two or more files.
The order of groups and of files inside a group does not matter.
Examples
Example 1
Input
duplicate_files({'file1.txt': 'abc123', 'file2.txt': 'xyz789', 'file3.txt': 'abc123', 'file4.txt': 'pqr456'})Expected output
[['file1.txt', 'file3.txt']]
+ 3 hidden tests on Submit.
Edge cases to ask about
- No duplicates
- Several groups
- Empty input
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.
Pick both to reveal the answer.
Measure it
Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| Compare every pair of files | O(n²) | O(1) | |
| bestGroup by hash | O(n) | O(n) | In real life: group by size first, hash only same-size files, in chunks. |
Walkthrough of the optimal approach (try it yourself first)
Invert the mapping into hash → [files], then keep groups with more than one file.
The real-world follow-up is computing the hashes: group by file size first (different sizes cannot be duplicates), then hash only the candidates with hashlib.sha256, reading in fixed-size chunks so a 4 GB file does not have to fit in memory.
Complexity: O(n) time, O(n) space. One pass to group, one over the groups.
Reveal the reference solution
def duplicate_files(hashes): by_hash = {} for name, digest in hashes.items(): by_hash.setdefault(digest, []).append(name) return [names for names in by_hash.values() if len(names) > 1]
Follow-ups interviewers ask
- Write the hashing step for a directory tree.
- Why group by size before hashing?
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Find Duplicate Files by Content Hash in Python?
The optimal solution runs in O(n) time and O(n) auxiliary space. One pass to group, one over the groups.
What is the brute-force approach, and how do you optimise it?
Compare every pair of files: O(n²) time, O(1) space. Group by hash: O(n) time, O(n) space. In real life: group by size first, hash only same-size files, in chunks.
What follow-up questions do interviewers ask about Find Duplicate Files by Content Hash?
Write the hashing step for a directory tree. Why group by size before hashing?
