KatInterview

Identify Duplicate Files Across a File System and Analyze System Bottlenecks

System DesignModel answer
MediumModel answerSystem Designfile-systemhashingio-performance

Last updated

Design an algorithm to find all groups of identical files in a directory tree while handling massive files, edge cases, and analyzing CPU versus disk I/O performance.

Design and implement an efficient program that accepts a root directory path, recursively scans the file system tree beneath it, and groups all regular files that have byte-for-byte identical content.

Core Requirements

  • Output each set of identical files as a grouped list of file paths.
  • Do not output unique files (files with no duplicates). If no duplicates exist anywhere in the directory tree, the output must be completely empty (no headers, no trailing newlines, no status messages).
  • The solution must scale cleanly to millions of files and files that exceed physical RAM capacity.

Key Considerations & Edge Cases

During your explanation, explicitly address:

  1. Symbolic Links & Hard Links: How should symlinks (and potential directory cycles) and hard links (files sharing the same inode) be handled?
  2. Special File Types & Permissions: How to handle empty files, unreadable files/directories, and files modified during execution?
  3. Collision Strategy: Is a cryptographic hash sufficient to determine identity, or is direct comparison required?
  4. Performance Analysis: Break down which stages are CPU-bound versus I/O-bound, and explain how to design a parallelized pipeline to optimize throughput.