KatInterview

Fewest-Hop Route Through Flaky Page Fetches

Trees & GraphsAuto-graded · 21 test cases
MediumAuto-gradedTrees & Graphsbfsgraphshortest-path

Last updated

Run BFS on a link graph to find the lexicographically smallest fewest-hop route, where some pages fail a set number of times before their links load.

Fewest-Hop Route Through Flaky Page Fetches

A crawler wants the shortest sequence of link clicks from an origin page to a destination page. Fetching a page's outgoing links over the network can fail temporarily, so the crawler retries. To keep runs deterministic, the network is modeled with two dictionaries.

Model

  • links_map[url] is the list of URLs that page url links to once its links load successfully. If url is not a key, the page has no outgoing links. A list may contain duplicates, and it may contain the page itself.
  • failed_attempts[url] is the number of consecutive failed fetches of url before the first successful one. If url is not a key, it is 0. A negative value is treated as 0.
  • The crawler may make at most 1 + retry_limit fetch attempts per page. A negative retry_limit is treated as 0, so there is always one attempt.
  • A fetch of url therefore succeeds exactly when failed_attempts[url] <= retry_limit. If it does not succeed, the page is treated as having no outgoing links.

Task

Implement find_fewest_hops_route(origin, destination, links_map, failed_attempts, retry_limit=3).

Return the route as a list of URLs that starts with origin and ends with destination, following these rules:

  1. Fewest hops. The route must use the minimum possible number of clicks (edges).
  2. Tie-break. If several routes share that minimum length, return the lexicographically smallest one. Compare the routes as lists of strings, element by element from the start, using ordinary string comparison.
  3. Failed pages. A page whose fetch does not succeed (see the model) is a dead end. The route may not continue through it, but the destination may still be reached by another route.
  4. Each page fetched once. Each page's links are looked up at most once during the whole search, so cycles must not cause repeated work or infinite loops. The destination's own links are never needed.
  5. Trivial case. If origin == destination, return [origin].
  6. No route. If the destination cannot be reached, return [].

Constraints

  • At most 10,000 distinct URLs are reachable from origin.
  • Each URL is a string of at most 256 characters.
  • retry_limit and the values in failed_attempts are integers, and either may be negative (treated as 0).
  • Link lists may contain duplicates and self-links.
  • origin and destination may be absent from links_map.

Examples

Example 1

Input: find_fewest_hops_route('snowflake.com/home', 'snowflake.com/careers', {'snowflake.com/home': ['snowflake.com/docs', 'snowflake.com/about'], 'snowflake.com/docs': ['snowflake.com/careers'], 'snowflake.com/about': ['snowflake.com/careers']}, {}, 3)

Output: ['snowflake.com/home', 'snowflake.com/about', 'snowflake.com/careers']

Example 2

Input: find_fewest_hops_route('a.com', 'b.com', {'a.com': ['b.com']}, {'a.com': 2}, 2)

Output: ['a.com', 'b.com']

Example 3

Input: find_fewest_hops_route('a', 'a', {'a': ['b']}, {}, 3)

Output: ['a']

Hints

Hint 1

Every click costs the same, so a breadth-first search gives the minimum number of hops. Decide first whether a page's links are available at all, using the failure count and the retry limit.

Hint 2

To get the lexicographically smallest route among the shortest, deduplicate and sort each page's links before queueing them. Then pages leave the queue in order of their routes.

Hint 3

Mark a URL as seen when you put it in the queue, not when you take it out. That guarantees each page is expanded once, and the first discovery of a URL comes via its best route. You can stop as soon as the destination is discovered.