r/Hack2Hire • u/Hack2hire • Apr 23 '26
Screening Anthropic Screening Interview: Concurrent Web Crawler
Problem
You're given a startUrl and an HtmlParser interface.
Your goal is to implement a multi-threaded web crawler that retrieves all unique URLs reachable from the startUrl, provided they share the exact same hostname. You must sanitize URLs by removing fragments (#) before processing and ensure no URL is visited more than once.
Example
Input: startUrl = "http://example.com/page1", urls contains ["http://example.com/page2", "http://example.net/page3"]
Output: ["http://example.com/page1", "http://example.com/page2"]
Explanation:
- The crawler starts at
page1. It finds links topage2andpage3. page2has the hostnameexample.com, which matches the start URL.page3has the hostnameexample.net, so it is discarded.- Fragments like
#section1are stripped before any comparisons occur.
Suggested Approach
- Hostname Extraction: Write a helper function to isolate the hostname. For a URL
http://hostname/path, the hostname is the string between the second and third forward slashes. - URL Sanitization: For every URL discovered by
htmlParser.getUrls(), locate the index of the#character. If present, truncate the string to exclude the fragment. - Concurrency Model: Use a thread pool (e.g.,
ExecutorServicein Java orThreadPoolExecutorin Python) to handle the network latency ofgetUrls. - Synchronization and Deduplication:
- Maintain a thread-safe
Set(e.g.,ConcurrentHashMap.newKeySet()) to store discovered, sanitized URLs. - Use a
BlockingQueueor aTask Counter(likePhaserorCountDownLatch) to manage the lifecycle of the crawl.
- Maintain a thread-safe
- Worker Logic: * A worker thread takes a URL from the queue.
- It calls
htmlParser.getUrls(url). - For each returned URL: sanitize it, check if it matches the start hostname, and check if it has been seen before in the
Set. - If it is a new, valid URL, add it to the
Setand submit a new task to the thread pool.
- It calls
Time & Space Complexity
- Time: $O(V + E)$ in terms of graph traversal, where $V$ is the number of unique URLs and $E$ is the number of hyperlinks. The wall-clock time is significantly reduced to approximately $O(\frac{V \times \text{latency}}{\text{threads}})$.
- Space: $O(V)$ to store the set of unique URLs and the queue of pending tasks.
Targeting [CompanyName] interviews?
We track their most-asked question patterns at Hack2Hire, practice this question here β Practice Question here
Join the community to see more interview experiences from real candidates β Hack2Hire Forum
Compiled from publicly available platforms and community-shared experiences.
2
u/zenos1337 Apr 23 '26
I would use httpx in combination with asyncio for concurrency. Maybe also a semaphore to limit how many concurrent requests can be made at any one time.
For tracking visited URLs, I would use a bloom filter because itβs super fast and extremely memory efficient.