Web crawler
Anthropic candidates report a web crawler question in coding rounds, written with threads or processes, sometimes with a design discussion on top (3 sources). One staff candidate describes the order it came in: crawl a site first, then make it multithreaded (1 report). It starts like this:
You get a start URL and a function
fetch(url)that returns the links on that page.fetchis slow, because it waits on the network, and it can raise.Write
crawl(start_url, fetch). Return the set of every URL on the same hostname asstart_urlthat you can reach from it,start_urlincluded. Drop#fragmentsbefore you compare URLs. Never fetch a page on another host, subdomains included. Fetch each URL once. A page whose fetch raises still counts as reached, with no links.For example, on a site of three pages:
fetch("http://example.com/")returns["http://example.com/a", "http://example.com/b#top", "http://other.com/x"]fetch("http://example.com/a")returns["http://example.com/", "http://news.example.com/n"]fetch("http://example.com/b")returns[]So
crawl("http://example.com/", fetch)returns{"http://example.com/", "http://example.com/a", "http://example.com/b"}. The#topis dropped, andother.comandnews.example.comare never fetched.
Once that works, the interviewer adds to it: fetch several pages at the same time, with a limit on how many are in flight, then retry a page that times out. The fetching isn't what they grade. They watch who keeps the set of pages already seen, how you know you're done when every page can add more work, and how you bound the work in flight.
You don't write it yet. The lesson first teaches what it needs, starting with why threads help a crawler at all. At the end you solve the whole problem, level by level, in an interview simulation with a 55-minute timer.
This lesson builds on the grid search: a crawl is a breadth-first search over links, with a seen-set from the hash maps lesson. The new idea here is threads, so that many slow fetches can wait at the same time.