Find Duplicate File in System
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

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
- What exactly counts as a duplicate? Same bytes (here), or same name, or same image after resizing?
- Should groups of one be returned? (Here: no.)
- Can names or contents contain spaces or parentheses? (Here: no; otherwise the format needs escaping.)
- For the real version: how large are the files and how many are there? That decides how much you can afford to read.
- Follow symlinks? Count hard links to the same file as duplicates? (Usually: no and no.)
Try it yourself first
You have the full problem. Sketch your answer, then sign up free to read the complete walkthrough.
- The complete design, flow by flow, with pictures
- Deep dives into the hard parts
- What mid, senior and staff candidates are expected to say
- Full company question lists
Free forever. No card needed.