KatInterview

Find the Exit of a Keyed Forest Graph

Trees & GraphsAuto-graded · 15 test cases
MediumAuto-gradedTrees & Graphsbfsgraphstate-space-search

Last updated

Run a breadth-first search over (location, keys held) states in a forest with locked doors to find the first exit reached, or None.

Forest Exit with Locked Doors

A forest is described by a list of location records. You move through it by following links from one location to another, and you must find an exit.

Each record is a dictionary with these fields:

  • id (string): the unique name of the location.
  • message (string, optional): a location is an exit exactly when message == "DONE".
  • neighbors (list, optional, default empty): outgoing links. Each link is a dictionary with:
    • url (string): looks like /mirkwood?location=B. The destination id is the value of the location query parameter.
    • direction (string, optional): informational only.
    • locked (boolean, optional, default false): whether the door is locked.
    • required_key (string, optional): the key needed to pass if the door is locked.
  • keys (list of strings, optional, default empty): keys picked up automatically upon arriving at this location.

Movement rules

  • You start at the location start_id. You collect that location's keys immediately.
  • Keys are never lost. Arriving at a location adds its keys to your collection.
  • A link with locked true can only be used if required_key is present and you already hold that key. Hold means you have it before crossing, including keys from the location you are standing on. A locked link with no required_key can never be used.
  • A link is ignored if its url has no non-empty location query parameter, or if that id does not exist among the records. If the parameter appears several times, use the first value.
  • Locations may be revisited when you hold a different set of keys than before, since that can open new doors. The same location with the same set of keys is never processed twice.

Search order

Explore shallow positions before deep ones with a breadth-first search over states (location id, set of keys held):

  1. Begin with the single state for start_id.
  2. Take states from a first-in-first-out queue. When a state is taken out, check whether its location is an exit. If so, return that location's id.
  3. Otherwise, go through the location's neighbors in the listed order, and for each usable link, compute the new state. If it has not been seen before, add it to the back of the queue.

Task

Implement find_forest_exit(rooms, start_room), which returns the id of the exit found first under the order above, or None if no exit can be reached, or if start_room is not one of the records.

Constraints

  • 0 ≤ number of locations ≤ 2000, and location ids are unique.
  • Each location has at most 20 neighbors and at most 10 keys.
  • The total number of distinct keys across the forest is at most 10.
  • Ids and key names are non-empty strings.
  • Links may form cycles and self-loops.

Examples

Example 1

Input: find_forest_exit([{'id': 'A', 'neighbors': [{'direction': 'north', 'url': '/mirkwood?location=B'}, {'direction': 'east', 'url': '/mirkwood?location=C'}]}, {'id': 'B', 'neighbors': [{'direction': 'north', 'url': '/mirkwood?location=Z'}]}, {'id': 'C', 'neighbors': [{'direction': 'east', 'url': '/mirkwood?location=Z'}]}, {'id': 'Z', 'message': 'DONE', 'neighbors': []}], 'A')

Output: 'Z'

Example 2

Input: find_forest_exit([{'id': 'A', 'neighbors': [{'url': '/mirkwood?location=B'}, {'url': '/mirkwood?location=C'}]}, {'id': 'B', 'neighbors': [{'url': '/mirkwood?location=Z'}]}, {'id': 'C', 'neighbors': [{'url': '/mirkwood?location=Z'}]}, {'id': 'Z', 'message': 'DONE', 'neighbors': []}], 'A')

Output: 'Z'

Hints

Hint 1

Think of the search state as a pair: the current location and the set of keys you hold. The same location with different key sets can behave differently.

Hint 2

Use a FIFO queue and a visited set of (location, frozenset of keys). Check for the exit when you remove a state from the queue, and add the new location's keys when you move into it.

Hint 3

Extract the destination with a URL parser (parse_qs on the query string). Skip links without a usable location, with an unknown id, or locked without the right key already held.