Baseten logoBaseten
CodingFree preview

Speeding 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.

SWE
Infra Eng
python
concurrency
threading
networking
http
hashing
integrity
io
medium
Frequency
Single report
Last asked
2026-01-30
Stage
phone-screen

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 returns Content-Length.
  • download_byte_range(index, fileURL, startByte, endByte) - downloads one chunk to part{i}.bin and 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 ETag or Content-MD5 headers.
  • Compute SHA-256 per chunk and compare to an expected value.
  • Verify the final file hash against the remote.
  • Validate Content-Range boundaries.
  • 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

  1. Fail fast: cancel remaining futures and return False on the first error; interviewers listen for the map versus as_completed distinction.
  2. Hash each chunk as it lands, before stitching, so a bad chunk never reaches the final file.
  3. Validate Content-Range so a full-file response to a ranged request is caught.
  4. Stream the stitch in fixed-size blocks; a 10 GB file with 8 chunks would otherwise mean 1.25 GB per read.
  5. Compare the final size to fileSize; one line catches truncation and duplication.
  6. Mention retries: per-chunk hashes let you re-download only the failed chunk.

Follow-ups

Preparation

  • Rewrite the parallel downloader from memory using submit and as_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.
Was this article helpful?

Comments

Sign in to join the discussion
Loading...