← Questions

Breadth-first web crawler with retries

0
AmazonCodingLast asked Mar 2026

A coding interview question asked in Amazon's technical phone screen, last asked Mar 2026. It tests bfs, http and error handling.

Write a crawler that visits every page of one website, breadth-first, starting from a URL. You work in your own editor with documentation allowed and AI tools not allowed. The crawler stays on the start URL's host, normalizes URLs so equivalent ones count once, retries pages that fail for a transient reason, and skips pages that fail for good. Return the URLs it visited successfully, in visit order.

Interface

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

  • crawl(start_url: str, fetch, extract_links) -> list[str]
  • fetch(url) -> str returns the page's HTML or raises TransientError or PermanentError.
  • extract_links(html) -> list[str] returns the raw hrefs in document order; they may be relative.
  • Normalizing: resolve relative links against the current page, drop the #fragment, lowercase the scheme and host, drop default ports (:80 for http, :443 for https), resolve . and .. segments, and use / for an empty path.
  • A page that raises TransientError is tried up to 3 more times (4 attempts in all), then skipped.

Example

start_url = "http://site.com"
pages:
  http://site.com/       -> links ["/slow", "/ok", "/ok#frag"]
  http://site.com/slow   -> fails transiently 4 times in a row
  http://site.com/ok     -> links ["/child"]
  http://site.com/child  -> no links

crawl(...)  ->  ["http://site.com/", "http://site.com/ok", "http://site.com/child"]
# /ok#frag is the same page as /ok; /slow is dropped after 4 failed attempts

Constraints

  • Up to 10^4 pages and 2 × 10^5 links; URLs up to 200 characters.

Clarifying questions

  • Is "same site" the exact host, or does it include subdomains?
  • Which URL differences count as the same page (trailing slash, query order, case in the path)?
  • Should retries back off, and is order still strictly BFS while a page is retried?
  • Should robots.txt or a page limit apply?

What a strong answer covers

  • A queue and a seen set; marking a URL seen when it is enqueued, not when it is fetched, so it is never queued twice.
  • One normalize function used before both the host check and the seen check, built on a URL library rather than string slicing.
  • Retry logic separated from the crawl loop (a fetch_with_retries helper), with permanent errors not retried.
  • O(pages + links) work; tests with fake fetch for duplicates, external links, fragments and each failure kind.

Follow-ups

  • Crawl with many workers at once while keeping a per-host rate limit.
  • Exponential backoff with jitter for transient failures.
  • Run across machines: partition URLs by hash and share the seen set.

Where it's asked

  • Amazon · Technical phone screen · Last asked Mar 202660 min · Live coding in a shared editor that does not run code, plus Leadership Principles questions · No AI

What it tests

bfs · http · error handling

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