KatInterview

Detect File Changes Using Content-Based Tree Hashing

Trees & GraphsAuto-graded · 23 test cases
HardAuto-gradedTrees & Graphshashingtree-traversaldiff-algorithms

Last updated

Tests hierarchical hashing, tree traversal, and efficient change detection across nested directory structures

You are building a version control tool that tracks changes between two repository snapshots. Each snapshot is represented as a dictionary mapping file paths to their contents.

Your task is to construct a content-addressable tree structure where:

  • Each file is represented by a hash of its contents
  • Each directory is represented by a hash computed from its children's names, types, and hashes
  • The hash computation must be deterministic (sorted by child name)

Using this tree structure, efficiently identify all files that were added, removed, or modified between two snapshots.

Implement the function:

def detect_file_changes(snapshot_a: Dict[str, str], snapshot_b: Dict[str, str]) -> List[List[str]]

Parameters:

  • snapshot_a: Dictionary mapping file paths to content strings (first snapshot)
  • snapshot_b: Dictionary mapping file paths to content strings (second snapshot)

Returns:

  • List of changes, where each change is [path, change_type]
  • change_type is one of: "ADDED", "REMOVED", or "MODIFIED"
  • Results must be sorted by path in lexicographic order

Path Format:

  • Paths use forward slashes as separators (e.g., "src/main.py")
  • Paths are relative and do not start with /
  • Each path component must be non-empty

Change Classification:

  • ADDED: File exists in snapshot_b but not in snapshot_a
  • REMOVED: File exists in snapshot_a but not in snapshot_b
  • MODIFIED: File exists in both snapshots with different contents

Edge Cases:

  • If a path changes from file to directory (or vice versa), treat the old files as REMOVED and new files as ADDED
  • If two directories have identical hashes, their entire subtrees are identical and can be skipped
  • Empty snapshots are valid inputs
  • Duplicate or conflicting paths (e.g., both "src" as a file and "src/main.py") should raise ValueError

Constraints

  • 0 ≤ total files across both snapshots ≤ 10,000
  • 0 ≤ file content length ≤ 1 MB
  • 1 ≤ path component length ≤ 255
  • Path depth ≤ 100
  • Use SHA-256 for hashing
  • Only report file-level changes (not directories)

Examples

Example 1

Input: detect_file_changes({}, {})

Output: []

Example 2

Input: detect_file_changes({'src/main.py': 'print(\'hello\')'}, {'src/main.py': 'print(\'world\')', 'README.md': 'Intro'})

Output: [['README.md', 'ADDED'], ['src/main.py', 'MODIFIED']]

Example 3

Input: detect_file_changes({'old.txt': 'data', 'src/a.py': 'a'}, {'src/a.py': 'a', 'new.txt': 'data'})

Output: [['new.txt', 'ADDED'], ['old.txt', 'REMOVED']]

Hints

Hint 1

Build an n-ary tree mirroring the directory hierarchy, where internal nodes are folders and leaves are files

Hint 2

Hash each file's content with a type prefix; hash each directory by combining sorted children (name + type + hash)

Hint 3

During diff, compare directory hashes first—if equal, skip the entire subtree to avoid unnecessary comparisons