AlgoMaster Logo

Design Multithreaded Web Crawler

medium8 min readUpdated August 31, 2026
Listen to this chapter
Unlock Audio

You are given a starting URL and an HtmlParser API. The parser returns every URL found on a page:

Design a multithreaded web crawler that visits every URL reachable from startUrl that has the same hostname as startUrl.

A hostname is the part of a URL between the scheme and the next /. For example, the hostname of http://news.yahoo.com/news/topics/ is news.yahoo.com.

Your crawler must:

  • Include startUrl in the result.
  • Follow only URLs with the same hostname as startUrl.
  • Crawl each URL at most once, even when the graph contains duplicate links or cycles.
  • Use multiple worker threads so independent pages can be fetched concurrently.
  • Return only after the complete reachable same-host graph has been explored.

The result may be returned in any order.

The judge supplies the HtmlParser implementation and calls your WebCrawler class. The small HtmlParser declaration in the starter code describes that API; do not modify its method signature.

The judge also preloads the standard concurrency, collection, and callback APIs for every supported language. You do not need to add import, include, using, or package statements.

Example 1:

Input:

Output:

Explanation: http://news.google.com/news is reachable, but its hostname differs from the hostname of startUrl, so it is not crawled.

Example 2:

Input:

Output:

Explanation: The duplicate link and cycle do not cause either page to be crawled more than once.

Constraints

  • 1 <= number of URLs <= 1000
  • 1 <= url.length <= 300
  • startUrl is a valid URL in the hidden web graph.
  • All URLs use the http:// or https:// scheme.
  • htmlParser.getUrls(url) is safe to call concurrently.
  • The hidden graph may contain duplicate links, cycles, and links to other hostnames.

Starter Code

Understanding the Problem

The URLs form a directed graph. Each page is a vertex, and every link returned by getUrls is an outgoing edge. The crawl is therefore a graph traversal with three extra requirements:

  • Several workers explore the graph at the same time.
  • Only vertices on the starting hostname belong to the traversal.
  • The crawler must detect when both the work queue and all active work are finished.

Checking the visited set and then adding a URL must be one atomic operation. If those steps are separate, two workers can both observe that a URL is absent and both crawl it.

An empty queue alone does not mean the crawl is complete. Another worker may still be parsing a page and may discover more URLs a moment later.

Synchronization Strategy

Use a fixed worker pool and shared crawl state:

  • A blocking work queue containing discovered URLs.
  • A visited set used to claim each URL exactly once.
  • A lock, concurrent set, or atomic insertion operation protecting that claim.
  • A pending count covering every queued or currently processing URL.

Initialize the state by marking and enqueueing startUrl, with pending = 1.

Each worker repeatedly:

  1. Takes one URL from the work queue.
  2. Calls getUrls outside the shared-state lock so slow parsing does not serialize the workers.
  3. Filters every returned URL by hostname.
  4. Atomically claims each same-host URL. A newly claimed URL increments pending before it is placed on the queue.
  5. Decrements pending after the current URL has been fully processed.

When pending becomes zero, no URL is queued and no worker can still discover more work. The workers can then be stopped and joined safely.

Correctness

startUrl is inserted into the visited set before any worker begins, so it always appears in the result.

Before a discovered URL is enqueued, its hostname is compared with the starting hostname. Therefore, no different-host URL is crawled or returned.

The visited-set insertion is atomic. Exactly one worker can successfully claim a particular URL, so every same-host URL is enqueued and parsed at most once, even with duplicate edges or cycles.

Every claimed URL increments pending before becoming available to workers. A worker decrements pending only after it has inspected all outgoing links. Therefore, pending counts all work that could still reveal another URL. It can reach zero only after the entire reachable same-host graph has been explored.

Finally, the main thread joins all workers before copying the visited set. The returned result is complete and no crawl work remains in the background.

Code

Complexity Analysis

Let V be the number of reachable URLs with the starting hostname and E be the total number of links returned while crawling those URLs.

  • Time complexity: O(V + E) total work, excluding parser latency.
  • Space complexity: O(V) for the visited set and work queue.

With W workers, independent parser calls can overlap. The wall-clock time depends on graph shape and parser latency, but the total graph-processing work remains O(V + E).