Design a Multithreaded Web Crawler
🕷️ What is a web crawler? It is a program that visits a web page, reads the links on it, and then visits those pages too, until there is nothing new left to visit. Search engines use crawlers to find pages, and companies use small ones to check their own sites for broken links. The slow part is waiting for pages to download, so real crawlers fetch many pages at once.
This is the design behind LeetCode 1242 (Web Crawler Multithreaded): start from one URL, stay on its hostname, and use several threads. Interviewers who ask for "a web crawler with a worker pool", "a crawler with a visited set" or "a polite crawler" are asking for the same design with a different spotlight. We build the core that all of them share, and cover each angle: the worker pool and the visited set in the Implementation, and politeness both as a rule we enforce (pages the site asks us to skip) and as a follow-up (how fast we may hit a host).
Requirements
"Design a web crawler. Given a start URL, crawl every page on the same host that you can reach from it. Use multiple threads."
The prompt sounds complete, but almost every word hides a decision. Which pages count as "the same host"? What does "reach" mean when pages link in circles? How do the threads share work without fetching a page twice? Ask before you code.
Clarifying Questions
You: How do I fetch a page? Do I write the HTTP code?
Interviewer: No. You get an HtmlParser with one method, get_urls(url), that downloads a page and returns the links on it. Treat it as slow: one call can take hundreds of milliseconds.
You: What does "same host" mean exactly? Is blog.example.com part of example.com?
Interviewer: No. Compare the hostname exactly: the part between http:// and the next slash. Subdomains are different hosts.
You: Two links can point to the same page with a different #section at the end. Are those the same page?
Interviewer: Yes. Drop the fragment before you compare. Don't normalize anything else.
You: Pages link to each other in cycles. Should a page ever be fetched twice?
Interviewer: Never within one crawl. Fetching is the expensive part, and the site owner doesn't want duplicate traffic either.
You: Some sites publish a robots.txt that says "don't crawl these paths". Do we respect that?
Interviewer: Yes. Assume you are given a list of disallowed URL prefixes. Don't fetch anything that starts with one of them, and don't report it.
You: In what order should the result be?
Interviewer: Any order is fine for the crawl itself. Return the list sorted so it is easy to check.
You: Is there a depth limit, a page limit, or a limit on how fast we hit the server?
Interviewer: Not for now. Crawl everything reachable. We can talk about rate limits later.
The answer about the parser shapes the whole design. A slow call that we make thousands of times is exactly the situation where threads pay off: while one thread waits for a page, others can wait for theirs. The CPU work (filtering a few links) is tiny next to the waiting.
💡 Interview tip: Ask how the page is fetched before anything else. If the interviewer hands you an interface, you know the network is out of scope and the interview is about coordination: the shared queue, the visited set, and stopping correctly.
Final Requirements
crawl(start_url)returns every URL reachable fromstart_urlby following links, sorted.- Only URLs whose hostname equals the start URL's hostname are crawled. Links to other hosts are ignored, so their pages are never fetched.
- A fragment (
#...) is dropped before a URL is compared, crawled or returned. - URLs that start with a disallowed prefix are never fetched or returned. A disallowed start URL gives an empty result.
- Each URL is fetched at most once per crawl.
- Pages are fetched by a fixed number of worker threads working at the same time.
crawlreturns only after every reachable page has been processed, and all its threads have stopped.
Out of scope
- Downloading and parsing HTML (the
HtmlParserdoes it). - Crawling the whole web across many machines (a system design question; see the end of Extensibility).
- Depth limits, page limits and per-host rate limits (we come back to them in Extensibility).
- Retrying failed downloads.