Find Duplicate Files in a Filesystem
Learn this problemProblem statement
Implement a deterministic snapshot adapter for find_dups(root). Given a starting directory root and a filesystem snapshot entries, traverse reachable entries with iterative breadth-first search and return groups of duplicate regular files.
Each entry is [path, kind, target, content, readable]:
kindisDfor a directory,Ffor a regular file, orLfor a symbolic link.- A symbolic link stores its destination path in
target. Other entries use an empty target. - A regular file stores its exact byte content in
content. Other entries use empty content. readableis1or0. An unreadable entry is skipped.
Directory children are the entries whose normalized parent path is that directory. Process children in lexicographic path order. Resolve symbolic-link chains; skip a broken link or a link cycle. Visit each resolved directory at most once, so directory links cannot make traversal infinite.
Include each resolved regular file at most once. If several reachable paths resolve to the same file, represent it by the lexicographically smallest path. Group different files only when their contents are byte-for-byte equal. A memory-bounded implementation may bucket by byte length, compute SHA-256 with a fixed one-megabyte buffer, and confirm equal-hash candidates byte-for-byte.
Return only groups containing at least two files. Sort paths inside each group lexicographically, then sort groups by their first path.
Function
findDuplicateFiles(root: String, entries: String[][]) → String[][]Examples
Example 1
root = "/"entries = [["/","D","","","1"],["/a.txt","F","","same","1"],["/b.txt","F","","same","1"],["/c.txt","F","","other","1"]]return = [["/a.txt","/b.txt"]]The two readable files with content same form one duplicate group. The third file has different content.
Example 2
root = "/r"entries = [["/r","D","","","1"],["/r/a","D","","","1"],["/r/a/one","F","","x","1"],["/r/two","F","","x","1"],["/r/a/back","L","/r","","1"],["/r/alias","L","/r/a/one","","1"]]return = [["/r/a/one","/r/two"]]The directory link back to /r is bounded by the visited-directory set. The link /r/alias and /r/a/one identify the same file, so that file appears once under the smaller path.
Example 3
root = "/r"entries = [["/r","D","","","1"],["/r/a","F","","same","1"],["/r/b","F","","same","0"]]return = []The unreadable file is skipped, leaving no content shared by two reachable files.
Constraints
1 <= entries.length <= 2000.- Every entry contains exactly five strings.
- Every path is unique, normalized, absolute, and contains no trailing slash except
/. rootnames a readable directory entry.- Every symbolic-link destination is a normalized absolute path.
- The sum of regular-file content lengths is at most
200000. readableis exactly0or1.