Design a Multithreaded Download Manager
⬇️ What is a download manager? It is the program that fetches big files for you. Instead of pulling a file down in one long stream, it cuts the file into pieces, fetches several pieces at the same time, and puts them back together on disk. If the connection drops, it retries the piece that failed. If you pause, it remembers which pieces it already has, so resuming does not start from zero.
This problem sits between a classic object design question and a concurrency question. The objects are simple: downloads, chunks, a queue. The hard part is that several worker threads touch the same downloads at once, and pause, resume and retries all change state while those threads are running. Interviewers use it to see whether you can keep shared state correct without serializing all the work behind one giant lock.
Requirements
"Design a download manager. It downloads large files using several threads, lets the user pause and resume, shows progress, and should survive a flaky connection."
The prompt mentions four features (parallel chunks, pause and resume, progress, retries), and each one hides a decision. These questions pin them down.
Clarifying Questions
You: How do we split a file? Does the server let us ask for a byte range?
Interviewer: Yes, assume every server supports range requests. Split each file into chunks of a fixed size. The last chunk can be shorter.
You: How many threads? One per download, or one shared pool?
Interviewer: A fixed pool of worker threads shared by all downloads. Several downloads can be active at once, and their chunks share the same workers.
You: What happens when fetching a chunk fails?
Interviewer: Retry it a few times. If it still fails, give up on that chunk, but let the other chunks of the file finish. The download ends up failed, and the user can press resume to try the missing chunks again.
You: What does pause mean for a chunk that is halfway through?
Interviewer: Let it finish. Just don't start new chunks for that download. On resume, only fetch what is missing.
You: How is progress measured?
Interviewer: Bytes in finished chunks. The UI turns it into a percentage.
You: What if the user adds the same URL twice?
Interviewer: Refuse it while the first one is still in the manager and not complete. After it completes, they can download it again.
You: Should we model the real network and disk?
Interviewer: Put them behind small interfaces. For the code, a simulated network is fine.
The third answer is the one that shapes the design most. "Fail the whole download on the first bad chunk" and "let the rest finish, then retry the failed ones" are different state machines, and the second one means a chunk needs its own state, not just the download.
💡 Interview tip: For any concurrency problem, ask what happens to work that is already in flight when the user changes their mind (pause, cancel, shut down). The answer decides where your workers check for state changes, and it is the question most candidates forget.
Final Requirements
- A download has a URL and a size in bytes. It is split into chunks of a fixed size; the last chunk may be shorter.
- A fixed number of worker threads fetch chunks from all downloads in parallel. A chunk is fetched by one worker at a time.
- A failed fetch is retried up to
max_retriesmore times. If every attempt fails, the chunk is marked failed and the other chunks continue. - A download is
QUEUED, thenDOWNLOADINGonce its first chunk starts,COMPLETEDwhen every chunk is done, andFAILEDwhen every chunk is done or failed and at least one failed. pausestops new chunks of a download from starting. Chunks in flight finish and are kept.resumequeues only the chunks that are not done. Resuming a failed download retries its failed chunks.- Progress is the number of bytes in finished chunks. The manager also counts fetch attempts per download.
- Adding a URL that is already in the manager and not complete is refused.
Out of scope
- Real HTTP, redirects, authentication and servers without range support.
- Bandwidth limits and download priorities (we come back to these in Extensibility).
- Surviving a crash or restart of the app (also in Extensibility).
- Verifying file checksums.