Design a system to detect duplicate data in a file
Difficulty: MEDIUMCategory: HLDFrequency: 1Asked On 2024-04-25
Problem Statement
Design a data de-duplication system that can process large files (potentially gigabytes or terabytes in size) and detect duplicate records, lines, or chunks of data. The system should be able to handle structured, semi-structured, and unstructured files, and work efficiently under memory, performance, and concurrency constraints.
🔶 Functional Requirements
1.Input File Handling
•Accept files of varying sizes (small → very large).
•Support text, CSV, log, and semi-structured formats.
2.Duplicate Detection
•Detect duplicates at line-level (exact duplicate lines).
•Detect duplicates at record-level (e.g., same user ID or transaction ID).
•(Optional) Detect near-duplicates (fuzzy matching).
3.Output
•Generate a report of duplicates (line numbers, counts).
•Option to remove duplicates and output a clean file.
•Support streaming mode for large files that cannot fit in memory.
4.Concurrency & Scaling
•Allow parallel processing of large files.
•Handle multiple files being checked concurrently.
5.Extensibility
•Support pluggable hashing/deduplication strategies.
•Configurable for case sensitivity, whitespace tolerance, etc.
🔶 Non-Functional Requirements
•Scalability: Must handle files up to TB scale.
•Performance: Duplicate detection should run in O(n) or better with hashing.
•Memory Efficiency: Cannot load entire file into memory → must use streaming/chunking.
•Concurrency Safety: Multiple threads should safely update shared structures (e.g., hash sets).
•Reliability: Must not miss duplicates even in large files.
•Fault Tolerance: Handle partial failures gracefully.
🔶 Constraints
•Files may not fit into memory (→ must use disk-based or streaming approach).
•Duplicate detection must work across distributed chunks.
•For extremely large files, hashing must avoid collisions and ensure correctness.
•Must ensure idempotency (same file always gives same duplicate report).
🔶 Example Use Cases
1.A CSV of user registrations contains duplicate user IDs → system detects and reports them.
2.A log file with repeated entries → system highlights duplicate log lines.
3.A transaction file with 1 billion rows → system uses chunk-based hashing to find duplicates.
4.A file processed concurrently by multiple workers → duplicates are correctly detected after aggregation.
5.A cleaned version of the file is generated with duplicates removed.
👉 Summary:
"Design a scalable and efficient system to detect duplicate data in large files, ensuring memory efficiency, concurrency safety, streaming support, and reliable deduplication across different file types."