Crawl Same-Domain Links
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of web crawling and link traversal algorithms, including URL normalization, same-domain filtering, deduplication, and cycle-safe graph traversal.
Constraints
- 0 <= len(web) <= 10^4
- The total number of hyperlinks across all pages is at most 2 * 10^5
- All URLs are absolute HTTP/HTTPS-style URLs
- Same-domain means exact hostname match after normalization
Examples
Input: ('https://example.com', {'https://example.com': ['https://example.com/about', 'https://other.com/', 'https://example.com/blog'], 'https://example.com/about': ['https://example.com', 'https://example.com/contact'], 'https://example.com/blog': ['https://example.com/about', 'https://blog.example.com/post'], 'https://example.com/contact': []})
Expected Output: ['https://example.com/', 'https://example.com/about', 'https://example.com/blog', 'https://example.com/contact']
Explanation: The crawler stays on hostname `example.com`, ignores `other.com` and `blog.example.com`, and safely handles the cycle back to the home page.
Input: ('HTTPS://Example.com#top', {'https://example.com/': ['https://example.com/docs#section1', 'https://example.com/docs', 'https://example.com/faq'], 'https://example.com/docs': ['https://example.com/#footer'], 'https://example.com/faq': ['https://example.com/docs#section2']})
Expected Output: ['https://example.com/', 'https://example.com/docs', 'https://example.com/faq']
Explanation: The start URL is normalized to `https://example.com/`. Fragments like `#top` and `#section1` are removed, so duplicate visits are avoided.
Hints
- Model the website as a graph: pages are nodes and hyperlinks are directed edges. A stack or queue plus a visited set is enough.
- Use URL parsing to extract the hostname and to normalize each URL before storing it, so fragments and case differences do not create duplicate visits.