Solve constrained monster traversal
ConfluentSoftware Engineer · Technical ScreenLast updated
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.
You are given a directed graph with n rooms labeled 0..n-1; each room i contains a monster with health hp[i] ≥ 0. You start at room s with energy E. Entering room i immediately reduces your energy by hp[i]; if energy ever becomes negative, you die. Part A: Determine whether there exists a path from s to target t such that energy never drops below 0 at any point; return true/false and one valid path if it exists. Implement using DFS with pruning and justify correctness and complexity. Part B: There are m potions; potion j sits in room pj and increases your energy by gain[j] the first time you enter pj. You may collect any potions along the way. Find a path from s to t that maximizes the number of rooms you can visit while keeping energy nonnegative, or report that t is unreachable. Discuss algorithmic approach (e.g., graph search with state, heuristics, or DP), correctness, and optimize for n up to 2e5 and edges up to 5e5.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.