Detect duplicate files by content
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates filesystem traversal, efficient I/O and memory use, hashing strategies for content comparison, duplicate-detection algorithms, and time/space complexity analysis.
Constraints
- 0 <= len(paths) == len(contents) == len(readable) == len(is_symlink) <= 10000
- Each paths[i] is unique.
- contents[i] is a string representing raw bytes; every character has code point 0 through 255.
- 0 <= len(contents[i]) <= 1000000
- The total content length in the test data is at most 5000000.
- Files with readable[i] == False or is_symlink[i] == True must be ignored.
Examples
Input: (['/root/a.txt', '/root/b.txt', '/root/c.txt', '/root/sub/d.txt', '/root/e.bin'], ['hello', 'world', 'hello', 'hello', 'hello!'], [True, True, True, True, True], [False, False, False, False, False])
Expected Output: [['/root/a.txt', '/root/c.txt', '/root/sub/d.txt']]
Explanation: Three readable regular files contain exactly 'hello'. The file containing 'world' differs, and 'hello!' has a different size.
Input: (['/a/empty1', '/b/empty2', '/a/img1', '/b/img2', '/c/text'], ['', '', 'ABC', 'ABC', 'ABD'], [True, True, True, True, True], [False, False, False, False, False])
Expected Output: [['/a/empty1', '/b/empty2'], ['/a/img1', '/b/img2']]
Explanation: The two empty files are duplicates, and the two files containing 'ABC' are duplicates. 'ABD' has the same size as 'ABC' but different content.
Input: (['/readable/original', '/secret/copy', '/link/copy', '/readable/other'], ['data', 'data', 'data', 'other'], [True, False, True, True], [False, False, True, False])
Expected Output: []
Explanation: The unreadable file and symbolic link are ignored, leaving no duplicate group among readable regular files.
Input: (['/x/one', '/x/two', '/x/three', '/x/four'], ['ab', 'ba', 'cd', 'dc'], [True, True, True, True], [False, False, False, False])
Expected Output: []
Explanation: All files have the same size, but none have identical content.
Input: ([], [], [], [])
Expected Output: []
Explanation: An empty directory scan contains no duplicate files.
Hints
- Files with different sizes cannot have identical byte content, so use file size as the first grouping key.
- Hashes are useful for reducing comparisons, but do not trust hashes alone; verify candidate duplicates byte-by-byte against a representative.