KatInterview

Level-by-Level Site Walk With Failing Pages and a Fetch Cap

Trees & GraphsAuto-graded · 18 test cases
MediumAuto-gradedTrees & Graphsbfsgraphlevel-order

Last 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 in graph.
  • max_concurrency (int, default 4): 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:

  1. Level 0 is just [start_url].
  2. Level k (for k >= 1) consists of every page that is linked from some page in level k - 1 (counting only pages that did not fail) and that has not already appeared in any earlier level.
  3. Inside each level, pages are sorted in ascending lexicographic (string comparison) order.
  4. 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_url itself is in failed_urls, the result is [start_url].
  • Pages that are linked but absent from graph appear in the result and are treated as having no links.

Constraints

  • 1 <= number of reachable pages <= 10,000
  • 1 <= 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.