Navigate Web Server Maze to Target Node
Trees & GraphsAuto-graded · 15 test casesLast updated
Evaluates breadth-first graph search and set tracking to traverse a network of web pages and find an exit target.
You are navigating a maze served by a web application. Each room in the maze corresponds to a distinct URL string.
You are provided with a starting room URL and a dictionary mapping available room URLs to their payload metadata. The metadata for each room contains two fields:
"is_exit"(bool):Trueif this room is the designated exit point, andFalseotherwise."neighbors"(List[str]): A list of accessible URL strings that lead from the current room to adjacent rooms.
The graph of rooms may contain cycles and dead ends (rooms with empty neighbor lists). Additionally, some referenced neighbor URLs might not be present in the dictionary.
Write a function that traverses the maze using standard graph search techniques to locate any reachable exit room. Return the URL of the first exit room discovered, or None if no reachable path to an exit exists.
Constraints
- The number of rooms in the network is between $0$ and $10^4$.
- Each neighbor list contains valid string URLs.
- Graph cycles are possible.
- If multiple exit rooms exist, returning any valid exit room URL reached via traversal is acceptable.
Examples
Example 1
Input: find_exit('https://maze.example/room/1', {'https://maze.example/room/1': {'is_exit': False, 'neighbors': ['https://maze.example/room/2', 'https://maze.example/room/7']}, 'https://maze.example/room/2': {'is_exit': False, 'neighbors': []}, 'https://maze.example/room/7': {'is_exit': True, 'neighbors': []}})
Output: 'https://maze.example/room/7'
Example 2
Input: find_exit('https://maze.example/start', {'https://maze.example/start': {'is_exit': False, 'neighbors': ['https://maze.example/a']}, 'https://maze.example/a': {'is_exit': False, 'neighbors': ['https://maze.example/start']}})
Output: None
Hints
Hint 1
Use Breadth-First Search (BFS) or Depth-First Search (DFS) to explore the web of rooms starting from the initial URL.
Hint 2
Keep track of visited URLs using a hash set to prevent infinite loops caused by cycles.
Hint 3
Check if a room URL exists in the given dictionary before trying to access its payload fields.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem asks us to find an exit room starting from a given initial URL in a graph of interconnected room objects. Since the graph may have cycles and missing nodes, we can use Breadth-First Search (BFS) (or Depth-First Search) combined with a hash set to track visited URLs.
- Initialize a queue with
start_urland a setvisitedcontainingstart_url. - While the queue is non-empty:
- Pop the current URL.
- Lookup the URL in
rooms. If it is missing, skip processing. - Check if
"is_exit"isTrue. If so, return the current URL. - Iterate over
"neighbors". If a neighbor has not been visited yet, mark it as visited and append it to the queue.
- If the queue becomes empty without finding any exit, return
None.
Complexity
- Time Complexity: $\mathcal{O}(V + E)$, where $V$ is the number of distinct room URLs and $E$ is the total number of neighbor links traversed. Each node and edge is processed at most once.
- Space Complexity: $\mathcal{O}(V)$ to store the queue and the visited set.
Python 3 Implementation
from collections import deque
from typing import Dict, Optional
def find_exit(start_url: str, rooms: Dict[str, Dict]) -> Optional[str]:
queue = deque([start_url])
visited = {start_url}
while queue:
url = queue.popleft()
room = rooms.get(url)
if room is None:
continue
if room.get("is_exit", False):
return url
for neighbor in room.get("neighbors", []):
if neighbor not in visited:
visited.add(neighbor)
queue.append(neighbor)
return None