Concurrent same-host web crawler
I was asked this
No reports yet. Sign in to say where you were asked it.
Implement a web crawler that finds every page reachable from a start URL on the same host, and fetches pages concurrently.
You are given a blocking helper:
def get_urls(url: str) -> list[str]:
"""Download the page at `url` and return the absolute URLs it links to.
Slow (network I/O) and may raise on failure."""
Write:
def crawl(start_url: str, get_urls, max_workers: int = 8) -> list[str]:
...
Rules
- Start at
start_url, and include it in the result. - Follow and return only URLs whose hostname is exactly the hostname of
start_url.http://docs.shop.iois a different host fromhttp://shop.io. - Ignore the
#fragmentpart of a URL:http://shop.io/cart#topandhttp://shop.io/cartare the same page. - Never fetch the same page twice, even when several threads find it at the same moment.
- If
get_urlsraises for a page, keep the page in the result and treat it as having no links. - Return the unique URLs in sorted order. The function must return once every reachable page has been fetched.
Start with a single-threaded version, then make it concurrent with at most max_workers fetches in flight. Explain how your crawler knows it is finished.
Constraints: up to 10,000 reachable pages; each fetch takes 10 ms to 2 s; links can form cycles.
Example 1
http://shop.io/ -> [http://shop.io/cart, http://shop.io/help, http://docs.shop.io/]
http://shop.io/cart -> [http://shop.io/, http://shop.io/help#returns]
http://shop.io/help -> [http://other.com/]
crawl("http://shop.io/", get_urls)
-> ["http://shop.io/", "http://shop.io/cart", "http://shop.io/help"]
Each of the three pages is fetched exactly once.
Example 2
If http://shop.io/ has no links, crawl("http://shop.io/", get_urls) returns ["http://shop.io/"].
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
Sign in to submit your code; every attempt is kept here.


