KatInterview

Navigate Web Server Maze to Target Node

Trees & GraphsAuto-graded · 15 test cases
EasyAuto-gradedTrees & Graphstrees-graphsbreadth-first-searchhash-map

Last 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): True if this room is the designated exit point, and False otherwise.
  • "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.