Find Duplicate File in System

Free with account Updated Nov 19, 2026 · 7 min read
On this page

    Problem statement

    You are given a list of directory listings. Each string is a directory path followed by one or more files written as name(content), separated by single spaces. Return every group of two or more files that have identical content, as lists of full paths (directory/name). Files with unique content are not returned; the order of groups and of paths inside a group doesn't matter. Follow-up: do the same on a real directory tree, where you can't read every file's content into memory.

    The parsing part takes a few minutes. The real interview is the follow-up: a dedupe tool for a real disk with large files, where the question is how little you can read. Original problem: LeetCode 609.

    Example

    Two files contain "sunset" and two contain "beach"; todo.txt is the only file with "milk", so it is left out.
    Two files contain "sunset" and two contain "beach"; todo.txt is the only file with "milk", so it is left out.
    Input:  ["photos a.jpg(sunset) b.jpg(beach)",
             "photos/2024 c.jpg(sunset)",
             "docs notes.txt(beach) todo.txt(milk)"]
    Output: [["photos/a.jpg", "photos/2024/c.jpg"],
             ["photos/b.jpg", "docs/notes.txt"]]
    Input:  ["root x.txt(1) y.txt(2)"]
    Output: []

    Constraints (assumed)

    • Up to 2 × 10⁴ listings, total input length up to 5 × 10⁵ characters.
    • Names contain no spaces or parentheses; contents contain no parentheses.
    • Follow-up: millions of files, some of them many gigabytes.

    What this question tests

    • Clean string parsing and choosing the right hash map key (the content, not the name).
    • Moving from a toy input to a real file system: sizes, partial hashes, streaming reads.
    • Discussing trade-offs: hash collisions, I/O cost, symlinks, files changing while you scan.

    Questions to ask first

    1. What exactly counts as a duplicate? Same bytes (here), or same name, or same image after resizing?
    2. Should groups of one be returned? (Here: no.)
    3. Can names or contents contain spaces or parentheses? (Here: no; otherwise the format needs escaping.)
    4. For the real version: how large are the files and how many are there? That decides how much you can afford to read.
    5. Follow symlinks? Count hard links to the same file as duplicates? (Usually: no and no.)