← Applied intuition Interview Insights
Start by clarifying that without reading contents, 'duplicate' can only mean metadata-identical files, and propose a multi-pass algorithm that first groups by size, then refines using other metadata like inode, timestamps, and permissions. Emphasize a robust traversal that handles symlink loops, hard links, and permission errors, and discuss scalable data structures and tradeoffs.
Pro tip: Mention that you would first check if the filesystem provides a built-in deduplication tool (e.g., fdupes) or if you can use file system APIs like extents or checksums stored in metadata, but since the question restricts to metadata, you'll design from scratch. This shows awareness of practical shortcuts while still solving the problem.
Clarify that without content, duplicates are files with identical metadata (size, timestamps, permissions, inode). Discuss that this is a heuristic and may produce false positives/negatives, and decide which metadata fields to use.
Use an iterative DFS with a stack to avoid recursion limits. Track visited directories by (device, inode) to prevent symlink loops and hard link cycles. Handle permission errors by logging and skipping.
For hard links, use (device, inode) to identify unique files; count links but treat as same file. For symlinks, either skip or resolve carefully with loop detection.
Use a hash map keyed by size to bucket files, then within each bucket, compare other metadata (e.g., mtime, permissions) to form candidate groups. Optionally use a composite key.
Discuss time O(N) for traversal and grouping, space O(N) for metadata storage. Consider memory vs. I/O tradeoffs: storing all metadata in memory vs. external sorting. Mention scalability with sharding or streaming.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.