Find an Exit in a URL Maze
Company: Ramp
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
Overview: This question evaluates graph traversal and state-management skills, emphasizing handling cycles, HTTP error conditions, retry logic, and propagation of authorization tokens in a networked client.
Read the full Ramp Software Engineer interview experience this question came from
Part 1: Find an Exit in a URL Maze with Cycle Detection
Constraints
- `0 <= len(edges) <= 2 * 10^5`
- `0 <= len(exits) <= 10^5`
- Each edge has exactly 2 strings: `[from_url, to_url]`
- The graph is directed and may contain cycles
- URLs are case-sensitive strings
Examples
Input: ('A', ['C', 'D'], [['A', 'B'], ['A', 'C'], ['B', 'D']])
Expected Output: 'C'
Explanation: BFS visits A, then B and C. C is the first exit reached.
Input: ('A', ['Z'], [['A', 'B'], ['B', 'C'], ['C', 'A']])
Expected Output: None
Explanation: The reachable part of the graph is a cycle with no exit.
Hints
- Use a queue to explore the maze level by level.
- Keep a `visited` set so a cycle like A -> B -> A does not loop forever.
Part 2: Find an Exit with Retries, 503/401 Handling, and Authorization Keys
Constraints
- `0 <= len(edges) <= 2 * 10^5`
- `0 <= len(exits) <= 10^5`
- `0 <= max_retries <= 10^6`
- For any room not present in `required_keys`, no key is required
- For any room not present in `keys_found`, no new key is acquired there
- For any room not present in `failures_before_success`, it has 0 transient failures
- At most one key is active at a time; a newly found key replaces the previous one
- The maze may contain cycles, so visited states should include both URL and current key
Examples
Input: ('A', ['D'], [['A', 'B'], ['A', 'D'], ['B', 'A']], {'D': 'k1'}, {'B': 'k1'}, {}, 0)
Expected Output: 'D'
Explanation: You must first visit B to get key k1, then revisit A and access D.
Input: ('S', ['E'], [['S', 'M'], ['M', 'E']], {}, {}, {'M': 2, 'E': 1}, 2)
Expected Output: 'E'
Explanation: Both M and E are accessible because their 503 counts are within the retry budget.
Hints
- A URL visited without a key is not the same state as the same URL visited later with a key.
- If `failures_before_success[url] > max_retries`, that room can never be accessed successfully, so you can prune it immediately.