Design Download Manager with Overlapping Segments Merge
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2019-06-28
Problem Statement
Designing a Download Manager that allows downloading large files in segments (for speed and reliability) and ensures overlapping segments are merged to avoid redundant downloads.
Focus is on modular design, data structures for segment management, and concurrent downloads.
✅ Functional Requirements
1.File Downloading
•Support large file downloads by splitting files into multiple segments.
•Each segment has: startByte, endByte, downloadedBytes.
•Segments can be downloaded concurrently.
2.Segment Management
•Track downloaded segments.
•Merge overlapping or contiguous segments to optimize storage and avoid redundant downloads.
3.Progress Tracking
•Provide overall download progress as a percentage.
•Allow querying for completed segments and pending segments.
4.Pause/Resume
•Ability to pause ongoing downloads and resume later without restarting completed segments.
5.Thread Safety
•Support multiple threads downloading different segments simultaneously.
•Ensure safe update and merge of segments.
🔐 Constraints & Assumptions
•Assume single-file downloads (can extend to multiple files).
•Segments can overlap due to retries or download logic.
•Focus is on LLD and data structures, not network/socket implementation.
•Memory-efficient segment storage is preferred.
🧱 Key Components & Classes
1.Segment Class
1
2class Segment {
3public:
4 long startByte;
5 long endByte;
6 long downloadedBytes;
7
8 Segment(long start, long end) : startByte(start), endByte(end), downloadedBytes(0) {}
9};1.DownloadManager Class
•Core manager to track segments, download progress, and merging.
1
2class DownloadManager {
3private:
4 long fileSize;
5 vector<Segment> segments; // Maintains all downloaded/active segments
6 mutex mtx; // Protects access to segments
7
8public:
9 DownloadManager(long fileSize);
10
11 void addSegment(long start, long end); // Adds new segment
12 void mergeSegments(); // Merge overlapping or contiguous segments
13 double getProgress(); // Returns total percentage downloaded
14 vector<Segment> getPendingSegments(); // Returns segments not fully downloaded
15};1.DownloadWorker Class
•Simulates downloading a segment in a separate thread.
1
2class DownloadWorker {
3private:
4 Segment segment;
5 DownloadManager* manager;
6
7public:
8 DownloadWorker(Segment seg, DownloadManager* mgr) : segment(seg), manager(mgr) {}
9
10 void run(); // Downloads the segment, updates manager
11};🔄 Example Workflow
1.File of size 1000 bytes → DownloadManager initialized.
2.Split into segments: [0-199], [200-399], [150-299], [400-999] (note overlap between 150-199).
3.Worker threads download segments concurrently → update downloadedBytes in manager.
4.mergeSegments() consolidates overlapping segments: [0-399], [400-999].
5.getProgress() calculates (downloadedBytes / fileSize) * 100.
🧪 Expected Deliverables
•Class diagram showing DownloadManager, Segment, DownloadWorker.
•Implementation of:
•addSegment
•mergeSegments
•getProgress
•Thread-safe updates for multiple worker threads.
•Test cases:
•Adding overlapping segments
•Concurrent updates to segments
•Merging contiguous and overlapping segments
•Correct progress calculation
💡 Discussion Points / Trade-offs
1.Segment Merge Strategy
•Merge immediately after every update → simple, frequent locking.
•Merge periodically → higher concurrency, slight overhead on calculation.
2.Thread Safety
•Use mutex for segments vector → simple but may block many threads.
•Fine-grained locking per segment → more complex but better concurrency.
3.Performance vs Memory
•Storing all segments individually → memory overhead but easy to track.
•Merge aggressively → reduces memory usage but more computation.
4.Extensibility
•Can extend to multiple files or resume downloads from disk.
•Can add retry logic for failed segments