Problem · String

Find Duplicate Files in a Filesystem

Learn this problem
HardHarvey logoHarveyFULLTIMEPHONE SCREEN

Problem 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]:

  • kind is D for a directory, F for a regular file, or L for 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.
  • readable is 1 or 0. 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 /.
  • root names a readable directory entry.
  • Every symbolic-link destination is a normalized absolute path.
  • The sum of regular-file content lengths is at most 200000.
  • readable is exactly 0 or 1.

More Harvey problems

drafts saved locally
public String[][] findDuplicateFiles(String root, String[][] entries) {
  // write your code here
}
root"/"
entries[["/","D","","","1"],["/a.txt","F","","same","1"],["/b.txt","F","","same","1"],["/c.txt","F","","other","1"]]
expected[["/a.txt", "/b.txt"]]
checking account