Quick Overview

Group file paths whose full contents are exactly equal, with deterministic sorting for paths and duplicate groups. The practical extension compares size filtering, hashing, and final byte verification to avoid unnecessary reads and hash-collision errors.

Group Duplicate Files by Content

Company: Anthropic

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

# Group Duplicate Files by Content Given a snapshot of files, return groups of paths whose complete contents are identical. The interview report preserves only the label "File Deduplication," so the literal records and ordering rules below are explicit practice assumptions rather than claimed original details. ```python def group_duplicate_files(files: list[list[str]]) -> list[list[str]]: ... ``` Each input record is exactly `[path, content]`. Paths are unique nonempty strings. Contents are arbitrary strings and may be empty. Return only groups containing at least two paths. Sort paths within each group lexicographically, then sort groups lexicographically by their complete path lists. Exact content equality defines a duplicate; path names and metadata do not. ## Example ```text Input: files = [ ["/a/report.txt", "same"], ["/b/empty.txt", ""], ["/c/copy.txt", "same"], ["/d/other.txt", "different"], ["/e/also-empty.txt", ""] ] Output: [ ["/a/report.txt", "/c/copy.txt"], ["/b/empty.txt", "/e/also-empty.txt"] ] ``` ## Constraints and Errors - `0 <= len(files) <= 200_000` - The total number of content characters is at most `20_000_000`. - A record with the wrong arity, non-string field, empty path, or duplicate path raises `ValueError`. - Validate the record shape and path uniqueness before producing output. - Do not mutate the input. ## Hints - A direct map from full content to paths is sufficient for this literal version. - For real files too large to hold in memory, first bucket by size, then by a streaming digest, and verify byte equality before declaring a duplicate. - Digest equality alone is not a proof of content equality when correctness must survive collisions.

Quick Answer: Group file paths whose full contents are exactly equal, with deterministic sorting for paths and duplicate groups. The practical extension compares size filtering, hashing, and final byte verification to avoid unnecessary reads and hash-collision errors.

Given [path, content] snapshot records, return deterministic groups of paths whose complete contents are exactly equal. Include only groups with at least two paths, sort paths within groups, and sort groups lexicographically.

Constraints

  • Each record is exactly [path, content].
  • Paths are unique nonempty strings.
  • Contents are arbitrary strings and may be empty.
  • Malformed records or duplicate paths raise ValueError.
  • Do not mutate the input.

Examples

Input: ([],)

Expected Output: []

Explanation: An empty snapshot has no duplicate groups.

Input: ([["/a", "x"], ["/b", "y"]],)

Expected Output: []

Explanation: Distinct contents produce no group.

Hints

  1. Validate all records and path uniqueness first.
  2. Map each exact content string to its paths.
  3. Sort each retained path group and then sort the list of groups.

Loading coding console...