Solve constrained monster traversal
Company: Confluent
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: Medium
Interview Round: Technical Screen
Quick Answer: Solve constrained monster traversal evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.
Constrained Monster Traversal — Part A: Survivable Path Existence
Constraints
- 1 <= n <= 2*10^5 (challenge inputs are small)
- 0 <= edges.length <= 5*10^5
- hp[i] >= 0 for all i
- 0 <= s, t < n
- E >= 0
- Energy must remain >= 0 after entering every room on the path, including s.
Examples
Input: (4, [[0,1],[1,2],[0,2],[2,3]], [0,2,1,1], 0, 3, 4)
Expected Output: (True, [0, 1, 2, 3])
Explanation: Start energy 4, enter 0 (cost 0) -> 4, enter 1 (cost 2) -> 2, enter 2 (cost 1) -> 1, enter 3 (cost 1) -> 0. Energy never negative, reaches t=3.
Input: (3, [[0,1],[1,2]], [0,5,0], 0, 2, 4)
Expected Output: (False, [])
Explanation: Only path 0->1->2 requires surviving room 1 (cost 5) with energy 4, which goes negative. No survivable path exists.
Input: (1, [], [0], 0, 0, 0)
Expected Output: (True, [0])
Explanation: s == t == 0, entering it costs 0 with energy 0; the trivial single-room path is valid.
Input: (1, [], [3], 0, 0, 2)
Expected Output: (False, [])
Explanation: Entering the start room itself costs 3 but energy is only 2, so you die on entry; no path.
Input: (5, [[0,1],[0,2],[1,4],[2,3],[3,4]], [0,10,1,1,0], 0, 4, 5)
Expected Output: (True, [0, 2, 3, 4])
Explanation: Branch 0->1 is pruned (cost 10 > energy 5). The survivable route is 0->2->3->4.
Input: (3, [[0,1]], [0,0,0], 0, 2, 100)
Expected Output: (False, [])
Explanation: Target room 2 has no incoming edge and is unreachable regardless of energy.
Hints
- Entering the start room s also costs hp[s]; the path is impossible immediately if E - hp[s] < 0.
- Because every hp[i] >= 0, walking through extra rooms can only lower your energy — so a simple path (no repeated rooms) is always at least as good as any walk that revisits rooms.
- Prune before recursing: only descend into a neighbor w if (current_energy - hp[w]) >= 0. This bounds the search to survivable states.
Constrained Monster Traversal — Part B: Maximize Rooms Visited with Potions
Constraints
- 1 <= n <= 2*10^5 (challenge inputs are small)
- 0 <= edges.length <= 5*10^5
- hp[i] >= 0; potion gain[j] >= 0
- Each potion [room, gain] applies once, on first entry to room
- Energy must remain >= 0 after entering every room, including s
- Return -1 if no survivable path reaches t
Examples
Input: (4, [[0,1],[1,2],[0,2],[2,3]], [0,2,1,1], [], 0, 3, 4)
Expected Output: 4
Explanation: No potions. The longest survivable path to t=3 is 0->1->2->3, visiting 4 rooms.
Input: (4, [[0,1],[1,2],[0,2],[2,3]], [0,5,1,1], [[1,5]], 0, 3, 4)
Expected Output: 4
Explanation: Room 1 costs 5 but holds a +5 potion (net 0 on entry), so 0->1->2->3 stays survivable and visits all 4 rooms; without the potion you'd be forced onto the shorter 0->2->3 (3 rooms).
Input: (3, [[0,1],[1,2]], [0,5,0], [], 0, 2, 4)
Expected Output: -1
Explanation: Room 1 costs 5 with energy 4 and there is no potion; t=2 is unreachable survivably, so -1.
Input: (3, [[0,1],[1,2]], [0,5,0], [[1,3]], 0, 2, 4)
Expected Output: 3
Explanation: Potion +3 in room 1 makes entry net -2 (4 -> 2), so 0->1->2 survives and visits 3 rooms.
Input: (1, [], [0], [], 0, 0, 0)
Expected Output: 1
Explanation: s == t == 0; the single-room path is trivially survivable, count = 1.
Input: (1, [], [3], [], 0, 0, 2)
Expected Output: -1
Explanation: Entering the only room (which is also t) costs 3 but energy is 2, so you die on entry; -1.
Input: (5, [[0,1],[0,2],[1,4],[2,3],[3,4]], [0,10,1,1,0], [[1,12]], 0, 4, 5)
Expected Output: 4
Explanation: Path 0->1->4 with the +12 potion survives but visits only 3 rooms; the longer survivable path 0->2->3->4 visits 4 rooms, which is the maximum.
Hints
- Precompute a per-room gain array from the potion list, then the energy delta on entering room r is simply gain[r] - hp[r].
- Potions are one-time and hp >= 0, so revisiting a room never helps. Restrict the search to simple paths (mark rooms on the current path).
- Track the running room count; whenever the DFS reaches t, update a global best. The answer is that best, or -1 if t is never reached survivably.