Find and remove duplicate files
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates the ability to design scalable file deduplication algorithms, specifically testing knowledge of hashing strategies, collision handling, memory and I/O optimization, handling very large files, incremental/resumable operation, and complexity and trade-off analysis.
Constraints
- 0 <= len(files) <= 200000
- Each path is unique
- 0 <= len(content) <= 100000 for each file
- The sum of all content lengths is at most 2000000 in this coding version
Examples
Input: ([('root/a.txt', 'abc'), ('root/b.txt', 'xyz'), ('root/c.txt', 'abc'), ('root/d.txt', 'xyz'), ('root/e.txt', 'p')], True)
Expected Output: ([['root/a.txt', 'root/c.txt'], ['root/b.txt', 'root/d.txt']], ['root/c.txt', 'root/d.txt'])
Explanation: Files root/a.txt and root/c.txt match exactly, and root/b.txt and root/d.txt match exactly. root/e.txt is unique.
Input: ([('a', 'cat'), ('b', 'cat'), ('c', 'dog')], False)
Expected Output: ([['a', 'b']], [])
Explanation: The duplicate group is still reported, but remove is False so no path is marked for deletion.
Hints
- Start by grouping files by length; files with different sizes can never be duplicates.
- Use a multi-stage fingerprint: cheap prefix/suffix key first, full hash next, and exact content equality last to protect against collisions.