Level-by-Level Site Walk With Failing Pages and a Fetch Cap
Trees & GraphsAuto-graded · 18 test casesLast updated
Compute a deterministic level-ordered listing of every page reachable from a start page, where failed pages yield no links and concurrency limits must not change the result.
Site Walk in Level Order
A site is modeled as a directed graph. Each page is a URL string, and each page links to zero or more other pages. You need to list every page reachable from a starting page, in a precise and repeatable order.
In a real system each page would be downloaded over a slow network, with several downloads running at once. Here the network is simulated with plain data, so the final list must be the same no matter how many downloads could run in parallel.
Inputs
start_url(str): the page where the walk begins.graph(Dict[str, List[str]]): maps a page to the list of pages it links to. A page that does not appear as a key has no outgoing links. A link list may contain repeats.failed_urls(List[str]): pages whose download fails. A failed page is treated as having no outgoing links, even if it has an entry ingraph.max_concurrency(int, default4): the maximum number of downloads that may be in flight at once. It is part of the interface, but it must not affect the answer.
Task
Return a list of all reachable pages, ordered as follows:
- Level 0 is just
[start_url]. - Level
k(fork >= 1) consists of every page that is linked from some page in levelk - 1(counting only pages that did not fail) and that has not already appeared in any earlier level. - Inside each level, pages are sorted in ascending lexicographic (string comparison) order.
- The result is level 0, then level 1, then level 2, and so on, concatenated.
Rules
- Every page appears in the result exactly once, so cycles and repeated links must not produce duplicates.
- A failed page still appears in the result when it is discovered. It just contributes no links.
- If
start_urlitself is infailed_urls, the result is[start_url]. - Pages that are linked but absent from
graphappear in the result and are treated as having no links.
Constraints
1 <= number of reachable pages <= 10,0001 <= max_concurrency <= 64- Each URL is a printable ASCII string of length 1 to 256.
Examples
Example 1
Input: walk_site('pageA', {'pageA': ['pageC', 'pageB'], 'pageB': ['pageD'], 'pageC': ['pageD', 'pageA'], 'pageD': []}, [], 2)
Output: ['pageA', 'pageB', 'pageC', 'pageD']
Example 2
Input: walk_site('root', {'root': ['child1', 'child2'], 'child1': [], 'child2': ['leaf'], 'leaf': []}, ['child1'], 4)
Output: ['root', 'child1', 'child2', 'leaf']
Hints
Hint 1
Process the walk one whole level at a time: a list for the current level, and a set of everything already seen.
Hint 2
Collect the next level into a set so repeated links and pages shared by several parents are merged, then sort it before appending to the output.
Hint 3
Mark pages as seen at the moment they are discovered, not when they are expanded, and skip expanding any page in failed_urls. max_concurrency never changes the answer.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Run a breadth-first search one level at a time.
- Keep
frontier, the current level (initially[start_url]), avisitedset, and aresultlist. - For each page in the frontier that did not fail, look up its links in
graph(missing pages have none). Add every link not yet invisitedto adiscoveredset. - Sort
discovered. This sorted list becomes the next frontier. Add it tovisitedand append it toresult. - Repeat until a level comes out empty.
In a real crawler, the downloads inside step 2 could run in a bounded thread pool with a barrier at the end of each level. The merge in steps 2 and 3 would still be done on a single thread. Because each level is computed as a set from the complete previous level and then sorted, completion order cannot influence the output. That is why max_concurrency is ignored here.
Why it is correct
- Before level
kis built,visitedholds exactly the pages from levels0..k-1, so filtering against it yields precisely the new pages one hop away. - Collecting into a set removes duplicates caused by repeated links or by several parents sharing a child.
- Pages are marked visited as soon as they are discovered, so each appears once and is expanded at most once. Cycles and back-edges are filtered out by
visited. - Failed pages are added to the result when discovered but skipped during expansion, so they contribute no links. Their children can still be reached through other pages.
Complexity
Let V be the number of reachable pages, E the number of links among them, and L the maximum URL length.
- Time:
O((V + E) * L)for the set operations, plusO(V log V * L)for sorting the levels. - Space:
O(V * L).
Reference solution
from typing import Dict, List
def walk_site(start_url: str, graph: Dict[str, List[str]], failed_urls: List[str], max_concurrency: int = 4) -> List[str]:
failed = set(failed_urls)
visited = {start_url}
result = [start_url]
frontier = [start_url]
_ = max_concurrency
while frontier:
discovered = set()
for url in frontier:
if url in failed:
continue
for link in graph.get(url, []):
if link not in visited:
discovered.add(link)
frontier = sorted(discovered)
visited.update(frontier)
result.extend(frontier)
return result