Design system for generating random UUIDs for files in a directory

Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2021-02-09

Problem Statement

🎯 Objective


Design a class-based system that scans a given directory, identifies all files, and assigns a unique, random UUID to each file.


The system should maintain a mapping between the file path and its UUID, ensuring no duplicates and allowing lookup or regeneration when required.


πŸ”Ή Context


In many systems (e.g., cloud storage, backup tools, or version control), files are assigned unique identifiers (UUIDs) for tracking, deduplication, or indexing.


You are required to design an in-memory or persistent UUID assignment module that can efficiently:


β€’Traverse directories and subdirectories
β€’Generate unique random UUIDs
β€’Maintain a mapping between files and their UUIDs
β€’Support querying, updating, and re-generation of UUIDs


βœ… Functional Requirements


1.The system should recursively scan all files in a given directory path.
2.For each file, generate a unique UUID (universally unique identifier).
3.Maintain a mapping of file_path β†’ UUID.
4.Support the following operations:
β€’Get UUID for a given file path
β€’Refresh or regenerate UUID for a given file
β€’Persist or export the mapping to disk (optional)
5.Ensure no two files share the same UUID.
6.Handle edge cases like deleted files, renamed files, or empty directories gracefully.


βš™οΈ Non-Functional Requirements


Aspect with Description


Performance

Should efficiently traverse directories with thousands of files.


Scalability

Should be extendable to handle large file systems.


Extensibility

Should allow support for different ID generation strategies (e.g., UUIDv4, hash-based).


Reliability

Each file must have a consistent and retrievable identifier.


Maintainability

Classes should be modular and follow SOLID principles.


🧱 Core Components / Classes


Class and Responsibility


FileScanner

Recursively scans directories and lists all files.


UUIDGenerator

Generates unique random UUIDs.


FileUUIDMapper

Maintains mapping between file paths and UUIDs.


UUIDPersistenceManager (optional)

Handles saving/loading mappings from a file or database.


Main / Client

Orchestrates the overall flow β€” scan, generate, map, persist.


🧾 Class Diagram (Text Representation)

1 2β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 3β”‚ FileScanner β”‚ 4│────────────────────────│ 5β”‚ + scanDirectory(path): List<File> β”‚ 6β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 7 β”‚ 8 β–Ό 9β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 10β”‚ UUIDGenerator β”‚ 11│────────────────────────│ 12β”‚ + generateUUID(): str β”‚ 13β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 14 β”‚ 15 β–Ό 16β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 17β”‚ FileUUIDMapper β”‚ 18│────────────────────────│ 19β”‚ - fileToUUID: Map β”‚ 20│────────────────────────│ 21β”‚ + assignUUID(file): void β”‚ 22β”‚ + getUUID(file): str β”‚ 23β”‚ + regenerateUUID(file): void β”‚ 24β”‚ + getAllMappings(): Map β”‚ 25β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 26 β”‚ 27 β–Ό 28β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” 29β”‚ UUIDPersistenceManager β”‚ 30│────────────────────────│ 31β”‚ + save(map): void β”‚ 32β”‚ + load(): Map β”‚ 33β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜


πŸ”„ Workflow Example


1.The client provides a directory path (e.g., /home/user/documents).
2.FileScanner lists all files in the directory and subdirectories.
3.For each file:
β€’UUIDGenerator generates a new UUID (e.g., 8f14e45f-ea...).
β€’FileUUIDMapper stores the mapping { "resume.pdf": "8f14e45f..." }.
4.The mapping can optionally be saved via UUIDPersistenceManager.
5.The user can query a file’s UUID anytime.


βš™οΈ Example Flow

1 2Input Directory: /user/photos 3Files Found: 4 - photo1.jpg 5 - photo2.jpg 6 - holiday/photo3.jpg 7 8Generated UUIDs: 9 - photo1.jpg β†’ 1a79a4d6-87e 10 - photo2.jpg β†’ e4da3b7f-bb6 11 - holiday/photo3.jpg β†’ 1679091c-5a8 12 13Mapping saved to: uuid_mapping.json


🧠 Key Design Considerations


β€’Consistency: Once generated, the same UUID should persist for a file until explicitly regenerated.
β€’Extensibility: Use a UUIDGenerator interface to support different strategies (random, hash-based, deterministic).
β€’Thread Safety: If the system runs in parallel, ensure synchronized access to the mapping.
β€’Error Handling: Handle unreadable directories, permission issues, or I/O errors gracefully.


🧩 Design Patterns Used


Pattern Role


Factory Method

To create UUID generators with different strategies.


Singleton

UUIDGenerator or FileUUIDMapper can be global instances.


Strategy Pattern

Different UUID generation strategies (UUIDv4, hash-based).


Repository Pattern

For persisting file–UUID mappings.


πŸ’¬ Example Interview Prompt Summary

Design a system that scans a directory and assigns unique random UUIDs to each file.
The system should maintain mappings, support queries and regeneration, and handle large directories efficiently.
Discuss class design, extensibility, and error handling.


πŸš€ Bonus Extension Ideas


β€’Add file watcher support to automatically assign UUIDs when new files appear.
β€’Add checksum-based UUIDs for deterministic mapping.
β€’Store mappings in a database instead of in-memory.
β€’Expose as a REST API for external lookup.


Solution

Comments