Solve constrained monster traversal
Problem Description
You are given a directed graph with n rooms, labeled 0 to n-1. Each room i contains a monster with health hp[i] >= 0. You start at room s with an initial energy E. Entering room i immediately reduces your energy by hp[i]. If your energy ever becomes negative, you die.
Part A: Reachability with Energy Constraint
Determine whether there exists a path from the starting room s to the target room t such that your energy never drops below 0 at any point.
Return true if such a path exists, false otherwise. If a path exists, also return one valid path.
Implement this using Depth-First Search (DFS) with pruning. Justify the correctness and complexity of your approach.
Part B: Maximizing Visited Rooms with Potions
In addition to the above, there are m potions. Each potion j is located in room pj and increases your energy by gain[j] the first time you enter room pj. You may collect any potions along the way.
Find a path from room s to room t that maximizes the total number of rooms you can visit while ensuring your energy remains non-negative throughout the journey. If room t is unreachable under these conditions, report that t is unreachable.
Discuss your algorithmic approach (e.g., graph search with state, heuristics, or dynamic programming), its correctness, and optimize your solution for graphs with n up to 2e5 rooms and edges up to 5e5.
AI interview prep
Core dilemma
Navigating a graph with a dynamically changing, state-dependent resource (energy) while optimizing for path existence (Part A) and maximum visited nodes (Part B), especially when state space can explode due to potions and unbounded energy.
3 tradeoffs and 3 stress tests locked
Interview prep
Unlock the interviewer X-Ray rubric, tradeoff matrix, stress tests, clarifying questions, and key concepts.
Solve constrained monster traversal
Problem Description
You are given a directed graph with n rooms, labeled 0 to n-1. Each room i contains a monster with health hp[i] >= 0. You start at room s with an initial energy E. Entering room i immediately reduces your energy by hp[i]. If your energy ever becomes negative, you die.
Part A: Reachability with Energy Constraint
Determine whether there exists a path from the starting room s to the target room t such that your energy never drops below 0 at any point.
Return true if such a path exists, false otherwise. If a path exists, also return one valid path.
Implement this using Depth-First Search (DFS) with pruning. Justify the correctness and complexity of your approach.
Part B: Maximizing Visited Rooms with Potions
In addition to the above, there are m potions. Each potion j is located in room pj and increases your energy by gain[j] the first time you enter room pj. You may collect any potions along the way.
Find a path from room s to room t that maximizes the total number of rooms you can visit while ensuring your energy remains non-negative throughout the journey. If room t is unreachable under these conditions, report that t is unreachable.
Discuss your algorithmic approach (e.g., graph search with state, heuristics, or dynamic programming), its correctness, and optimize your solution for graphs with n up to 2e5 rooms and edges up to 5e5.