Find Duplicate Files by Content
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
### Problem
You are given a list of directory descriptions. Each description contains a directory path followed by one or more file records of the form `filename(content)`. Return every group of files with exactly equal contents. Include all full paths belonging to each group and omit groups containing only one file. A full path is `directory/filename`.
Both the order of the groups and the order of paths within each group may be arbitrary. The input already contains file contents; this task does not read an actual filesystem.
### Input Grammar
Use this explicit interpretation of the source's directory/file format:
```text
description = directory SPACE file_record (SPACE file_record)*
file_record = filename "(" content ")"
```
The directory may be just `root` or a nested path such as `root/a/b`. A directory path contains no spaces; parentheses in the directory path are ordinary path characters. A filename contains no spaces or parentheses. A single space outside parentheses separates the directory from its first file record and separates successive file records.
Content may contain spaces, including consecutive, leading or trailing spaces. Preserve every content character exactly: `a b`, `ab` and `a b` are different contents. Parentheses are structural delimiters in this grammar, so content contains no literal parentheses. The grammar also parses `file.txt()` as empty content.
The source does not define an escaping or nesting convention for literal parentheses inside filenames or contents. Such a convention would need to be specified before supporting that additional variant. Do not assume that a space inside parentheses ends a file record. Inputs are well formed under the grammar above; malformed-input handling is not required.
### Examples
**Example 1**
```text
Input: ["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)", "root 4.txt(efgh)"]
Output: [["root/a/2.txt", "root/c/d/4.txt", "root/4.txt"], ["root/a/1.txt", "root/c/3.txt"]]
```
The content `efgh` occurs in three files, and `abcd` occurs in two. Both complete groups must appear.
**Example 2**
```text
Input: ["root/a 1.txt(abcd) 2.txt(efgh)", "root/c 3.txt(abcd)", "root/c/d 4.txt(efgh)"]
Output: [["root/a/2.txt", "root/c/d/4.txt"], ["root/a/1.txt", "root/c/3.txt"]]
```
There are two groups of two files. If no content appears more than once, return an empty list.
**Example 3: spaces inside content**
```text
Input: ["root a.txt(a b) b.txt(a b) c.txt(ab) d.txt(a b)"]
Output: [["root/a.txt", "root/b.txt"]]
```
The first two files contain the same three-character string `a b`. Neither `ab` nor `a b` is equal to that content.
### Constraints
- `1 <= len(paths) <= 20,000`.
- Each description has `1..3000` characters, and the combined length is `1..500,000` characters.
- Input characters are English letters, digits, `/`, `.`, `(`, `)` and spaces, subject to the grammar above.
- Each description represents a different directory and contains at least one file record.
- Within a directory, no two files or directories share the same name.
### Interface and Existing Console
The original source uses `Solution.findDuplicate(paths)`. The written Python reference below provides a standalone `solution(descriptions)` function so that its complete implementation can be tested in isolation; its argument is the same list of descriptions.
The existing console on this page is a legacy version. It uses `solution(descriptions)`, limits input to `10,000` descriptions and `100,000` files, and requires sorted paths within groups and groups sorted by their first path. Its saved Python reference splits descriptions on whitespace and does not correctly handle the space-containing content above.
This written exercise preserves the broader source bounds and arbitrary output order. It does not change the legacy console or certify its grader, tests or references for this grammar. Use the written exercise independently; a successful legacy-console submission does not establish coverage of these requirements.
### What a Strong Answer Covers
- Return every member of every duplicate-content group, including different filenames with equal contents.
- Construct full paths correctly for root and nested directories.
- Preserve spaces inside content and distinguish content equality from equal names or equal sizes.
- Omit singleton groups and accept arbitrary ordering at both levels.
- Account for parsing and the storage required by all constructed full paths.
### Follow-up Questions
- For a real filesystem, would you discover files with DFS or BFS, and why?
- How would your approach change for gigabyte-sized files?
- What would you change if each read could return at most 1 KB?
- What are the time and space costs of that filesystem approach? Which operations consume the most time and memory, and how would you reduce those costs?
- How would you ensure that the reported duplicates are not false positives?
Overview: Group files with identical contents from directory descriptions, then explain how your approach would handle a large filesystem.
You are given a list of directory descriptions. Each description has the format:
root_directory file_name_1(content_1) file_name_2(content_2) ...
For example, the description '/data/a 1.txt(abcd) 2.txt(efgh)' means the directory '/data/a' contains the files '/data/a/1.txt' with content 'abcd' and '/data/a/2.txt' with content 'efgh'.
Your task is to return all groups of file paths whose file contents are identical. Only include groups that contain at least two files.
For this problem, return the result in a deterministic order: sort each group of file paths lexicographically, then sort the list of groups by the first path in each group. If there are no duplicate files, return an empty list.
Constraints
- 1 <= len(descriptions) <= 10^4
- The total number of files across all descriptions is at most 10^5
- Directory names and file names do not contain spaces
- File content does not contain parentheses
Examples
Input: (['/data/a 1.txt(abcd) 2.txt(efgh)', '/data/b 3.txt(abcd)', '/data/c 4.txt(efgh) 5.txt(xyz)'],)
Expected Output: [['/data/a/1.txt', '/data/b/3.txt'], ['/data/a/2.txt', '/data/c/4.txt']]
Explanation: Files with content 'abcd' are '/data/a/1.txt' and '/data/b/3.txt'. Files with content 'efgh' are '/data/a/2.txt' and '/data/c/4.txt'. 'xyz' appears only once, so it is excluded.
Input: (['/docs a.txt(hello) b.txt(world)'],)
Expected Output: []
Explanation: Both files have unique contents, so there are no duplicate groups.
Hints
- A hash map from file content to a list of full file paths lets you collect duplicates efficiently.
- For each file token like '1.txt(abcd)', find the first '(' to split the file name from its content.