Build a BFS Web Crawler
Company: Amazon
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Quick Answer: This question evaluates understanding of breadth-first traversal, URL normalization, domain scoping, duplicate detection, and retry/error handling, measuring algorithmic design and robustness in networked code.
Constraints
- `0 <= len(pages) <= 10^4`
- The total number of raw links across all pages is at most `2 * 10^5`
- Each URL length is at most 200 characters
- `failures[url]` is either `-1` or a non-negative integer
- All keys in `pages` and `failures` refer to normalized absolute URLs
Examples
Input: ('https://Example.com:443/a/index.html#top', {'https://example.com/a/index.html': ['../about', '/contact#team', 'https://other.com/home', '../about#bio', '/a/./products'], 'https://example.com/about': ['team', '/contact', 'https://example.com:443/a/index.html'], 'https://example.com/contact': ['/missing', '/about#again'], 'https://example.com/a/products': [], 'https://example.com/team': []}, {'https://example.com/contact': 1, 'https://example.com/missing': -1})
Expected Output: ['https://example.com/a/index.html', 'https://example.com/about', 'https://example.com/contact', 'https://example.com/a/products', 'https://example.com/team']
Explanation: The crawler normalizes the start URL, ignores the external domain, deduplicates repeated links to /about, retries /contact once before succeeding, and skips /missing because it permanently fails.
Input: ('http://site.com', {'http://site.com/': ['/slow', '/ok', '/ok#frag'], 'http://site.com/slow': [], 'http://site.com/ok': ['/child'], 'http://site.com/child': []}, {'http://site.com/slow': 4})
Expected Output: ['http://site.com/', 'http://site.com/ok', 'http://site.com/child']
Explanation: The root page is visited first. /slow is discovered before /ok, but it needs 4 transient failures before success, which exceeds the retry limit of 3 retries, so it is skipped. /ok and then /child are visited.
Hints
- Use a queue for BFS, and add a URL to the seen set when you enqueue it, not when you dequeue it.
- Normalize every link before both hostname checking and duplicate detection; otherwise equivalent URLs may be crawled more than once.