← 返回 snapchat 的题目列表Find Duplicate Files Without a Directory-Walking Utility
类型:online_judge
Given a root path root in a local file system, find all duplicate files in that directory and all of its subdirectories.
Two files are duplicates if and only if their byte contents are exactly identical. Return every duplicate group, where each group contains at least two full file paths.
Interview constraint: you must not use a library API that recursively walks the entire directory tree in one call, such as Java's Files.walk. Implement the traversal yourself, for example with an explicit stack or queue.
To avoid unnecessary file reads:
You may first group files by size; only files with the same size can be duplicates.
Read candidate files and compute a hash, then group them by hash.
Ignore symbolic links and do not follow symbolic links into directories.
Within each duplicate group, sort paths lexicographically. Sort all groups by their smallest path.
Input
Standard input contains one line:
root
root is the path of an existing directory.
Output
Print one line per duplicate group. On each line, print lexicographically sorted full paths separated by |.
Print nothing if there are no duplicate files.
Constraints
The directory tree contains at most 100,000 files and directory entries.
A single file can be as large as 1 GB.
File contents must be read in chunks; do not load an entire file into memory.
Example
Assume the following directory structure:
/tmp/data/
├── a.txt # content: hello
├── b.txt # content: world
├── sub/
│ ├── c.txt # content: hello
│ └── d.txt # content: world
└── unique.txt # content: different
Input:
/tmp/data
Output:
/tmp/data/a.txt | /tmp/data/sub/c.txt
/tmp/data/b.txt | /tmp/data/sub/d.txt
Example
Input
Fixture setup:
/tmp/t1/a.txt = "hello"
/tmp/t1/b.txt = "world"
/tmp/t1/sub/c.txt = "hello"
/tmp/t1/sub/d.txt = "world"
/tmp/t1/unique.txt = "different"
stdin:
/tmp/t1
Output
/tmp/t1/a.txt | /tmp/t1/sub/c.txt
/tmp/t1/b.txt | /tmp/t1/sub/d.txt