← Questions

Deduplicate concurrent downloads of the same URL

0
UberCodingLast asked Apr 2026

A coding interview question asked in Uber's coding round, last asked Apr 2026. It tests concurrency, threads and caching.

Write a thread-safe downloader that many threads call with URLs at once. Downloads of different URLs run independently and in parallel. When several threads ask for the same URL while its download is still in progress, only one real download happens: the other callers wait, and all of them get the same result when it finishes. The last part extends the design to firing a very large number of delayed events on time.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • Downloader(fetch: Callable[[str], bytes]) — fetch does the real network call, may be slow and may raise.
  • download(url: str) -> bytes — blocks until the content is ready. If the shared download fails, every waiting caller gets the same exception.
  • Once a download finishes, success or failure, the next call for that URL starts a new download (no result cache in the base version).

Example

t=0    thread 1: download("a")   ->  starts fetch("a")
t=1    thread 2: download("a")   ->  waits on the same fetch
t=1    thread 3: download("b")   ->  starts fetch("b") in parallel
t=5    fetch("a") returns X      ->  threads 1 and 2 both get X; fetch("a") ran once
t=6    thread 4: download("a")   ->  starts a new fetch("a")

Constraints

  • Hundreds of concurrent callers; thousands of distinct URLs.
  • No busy waiting.

Clarifying questions

  • On failure, should waiters share the error or each retry on their own?
  • Should finished results be cached, and for how long?
  • What timeout and cancellation do callers need? If one waiter gives up, does the download stop?
  • Can the same process have so many distinct URLs that memory matters?

What a strong answer covers

  • A map from URL to an in-flight future, guarded by a lock; the first caller creates the future and runs the fetch, later callers wait on it.
  • The check-and-insert done atomically, so two threads never both start a download (setdefault under a lock, computeIfAbsent, or similar).
  • Removing the entry when the download completes, in a finally, so failures do not leave a stuck entry and the next call retries.
  • Downloading outside the lock, so different URLs never block each other.
  • Tests: many threads on one URL with a counting fake fetch, a failing fetch, a slow fetch alongside a fast one.

Follow-ups

  • Implement it in Java with CompletableFuture and ConcurrentHashMap.
  • Downloads now run in several processes or machines. How do you deduplicate across them?
  • One hot URL returns a huge file. How do you avoid holding many copies in memory?
  • Fire millions of delayed events, each at its own time, accurately: compare one timer per event, a priority queue with a single scheduler thread, and a hierarchical timing wheel. How do you keep them across restarts?

Where it's asked

  • Uber · Coding · Last asked Apr 202660 min · Two rounds; one is often object-oriented design (a parking lot, a meeting scheduler, an expression evaluator) · No AI, except a newer AI-assisted debugging round in some senior loops

What it tests

concurrency · threads · caching

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding