← 返回 anthropic 的题目列表File Deduplication (find duplicate files)
类型:online_judge
File Deduplication (File Dedup)
Given a directory tree in a file system, implement a program to find duplicate files with identical content, and output groups of duplicate file paths (each group contains all paths whose file content is identical).
Task
Input: a directory tree (conceptually starting from a root path) containing files and subdirectories.
Find all duplicates: two or more files are considered duplicates if their byte contents are exactly identical.
Output: group duplicate files by content; only output groups with size >= 2.
Output format (example convention)
Print one line per duplicate group, with paths separated by spaces. Ordering of groups and ordering within a group do not matter.
Constraints / Requirements
You need to traverse the directory tree.
Handle large files: avoid reading entire files into memory at once.
Follow-up discussion:
How would you optimize if the system is I/O bound (disk reads are the bottleneck)?
How would you optimize if the system is CPU bound (hashing/comparison is the bottleneck)?
Example
Files:
/a/1.txt content "hello"
/b/2.txt content "hello"
/c/3.txt content "world"
Possible output:
/a/1.txt /b/2.txt
Example
Input
<root>=./example_dir (contains /a/1.txt='hello', /b/2.txt='hello', /c/3.txt='world')
Output
/a/1.txt /b/2.txt