Treasure Hunter
Reported by candidates from Persona's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Persona put Treasure Hunter in front of candidates in May 2022, and it looks like a grid maze but it isn't one. It reduces to a shortest-path search where the state isn't just your position. You also have to remember which traps and potions you've already triggered. With at most 15 special cells, that history fits in a bitmask. Then BFS gives you the minimum steps, and health is the tiebreaker. If you've got an OA coming, this is the shape to recognize. StealthCoder sits invisibly on screen as a safety net if the state design slips away mid-assessment.
The problem
You start at the top-left cell of a dungeon with 5 health. Move orthogonally through spaces. X is a wall, a digit is a trap that deals that much damage, H is a potion that restores 10 health, and T is the treasure. A trap or potion applies only the first time that path enters its cell. Health must remain positive. Return [minimumSteps, maximumRemainingHealth], maximizing health only among minimum-step surviving paths. Return [-1, 5] when no path reaches a treasure. Function treasureHunter(dungeon: String[]) → int[] Examples Example 1 dungeon = [" X","2T"] return = [2,3] The two-step path triggers damage 2 and reaches the treasure with health 3. Example 2 dungeon = [" H"," XX"," 9T"] return = [8,6] The shortest surviving route first takes the potion, then survives the damage-9 trap with health 6. Example 3 dungeon = [" X","XT"] return = [-1,5] Walls isolate the treasure. Constraints 1 <= rows, columns <= 12. The grid contains spaces, X, digits, H, and zero or one T. The top-left cell is a space. At most 15 trap and potion cells appear.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is state, not the grid. Plain BFS on (row, col) fails because a trap or potion only applies the first time a path enters it, so two paths to the same cell can have different histories. Index each trap and potion cell 0 to 14, and run BFS on (row, col, mask). Health is determined by the path, so store the best health seen per state, and only revisit a state if health improves at the same step count. Process level by level. When any level reaches T, return that step count and the max health among those arrivals. The common pitfall is applying damage on every re-entry, or keeping a plain visited set that throws away a better-health path. Also check health stays above zero after each trap. With a 12x12 grid and 2^15 masks, this is fine. If you blank on the mask idea during the live OA, StealthCoder is the hedge.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Treasure Hunter cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Persona's OA.
Persona reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Treasure Hunter FAQ
What's the trick in Treasure Hunter?+
Add a bitmask of triggered trap and potion cells to the BFS state. Position alone isn't enough because each special cell only applies once per path. State becomes (row, col, mask), and you track best health per state.
Why not just use Dijkstra on health?+
Steps are the primary goal and health is only a tiebreaker among minimum-step paths. Unit-cost BFS by level handles steps cleanly. Then you keep the max health among arrivals at the treasure on the first level where it's reached.
How big is the state space really?+
The grid is at most 12x12, which is 144 cells, and there are at most 15 trap and potion cells, so 32768 masks. That's about 4.7 million states at worst. It's fine for BFS if you encode the state compactly.
What edge cases should I test?+
Return [-1, 5] when there's no treasure or walls block it. Test a trap that would drop health to zero or below, since health must stay positive. Also test a potion that makes a later trap survivable, like Example 2.
How do I prepare for this in 48 hours?+
Write one BFS with a bitmask state from scratch, then one with a best-value-per-state array. Run the three examples by hand. Focus on the revisit rule: only re-expand a state when health improves, not on any revisit.