BasetenSpeeding Up a Large File Download and Verifying It
Modify starter code that downloads a file sequentially in batches using HTTP Range requests so that the chunks download in parallel, then add integrity verification.
Problem
You are given starter code that downloads a file sequentially in batches using HTTP Range requests and then stitches the parts together. Modify it to download the chunks in parallel, then add integrity verification.
Provided helpers:
get_file_size(fileURL)- issues a HEAD request and returnsContent-Length.download_byte_range(index, fileURL, startByte, endByte)- downloads one chunk topart{i}.binand returns whether it succeeded.download_file_in_batches(fileURL, numBatches)- the sequential loop to be replaced.
Part 1: parallelize downloads
Implement download_file_in_batches_parallel(fileURL, numBatches, max_workers=4). Split the file into numBatches byte ranges (the last chunk absorbs the remainder), download them concurrently, and stitch the parts in order into downloaded_file.bin, removing the part files. Return False if any chunk fails.
Part 2: verify integrity
Extend the downloader to detect corrupt or incomplete data. Ideas the interviewer expects you to discuss:
- Check
ETagorContent-MD5headers. - Compute SHA-256 per chunk and compare to an expected value.
- Verify the final file hash against the remote.
- Validate
Content-Rangeboundaries. - Check the total file size.
The framing resembles an S3-sync style download, and the starter code is boilerplate-heavy. The follow-up is phrased as: how do you check that each chunk download, and the final file assembly, match what was expected?
Hints
Pitfalls
- The round is engineering-flavored rather than algorithmic. Budget time to read the provided boilerplate before changing anything; the sequential download path is already there and only needs to be parallelized.
Talking Points
- Fail fast: cancel remaining futures and return
Falseon the first error; interviewers listen for themapversusas_completeddistinction. - Hash each chunk as it lands, before stitching, so a bad chunk never reaches the final file.
- Validate
Content-Rangeso a full-file response to a ranged request is caught. - Stream the stitch in fixed-size blocks; a 10 GB file with 8 chunks would otherwise mean 1.25 GB per read.
- Compare the final size to
fileSize; one line catches truncation and duplication. - Mention retries: per-chunk hashes let you re-download only the failed chunk.
Follow-ups
Preparation
- Rewrite the parallel downloader from memory using
submitandas_completed, with a dict keyed by chunk index, in 15 minutes. - Add a simulated failing chunk and confirm the function returns promptly and leaves no final file.
- Implement a streaming SHA-256 helper and a block-wise stitch, then test a file whose size is not divisible by
numBatches.
