Reported October 2024
Personabreadth first search

Escape the Haunted Castle with Treasures

Reported by candidates from Persona's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Persona OA. Under 2s to a working solution.
Founder's read

The mistake that sinks most first attempts at this Persona question, reported in October 2024, is treating it like a plain shortest-path problem and stopping once you have the move count. The task wants two numbers: the minimum moves to the escape point, and the most treasures you can grab on any route of exactly that length. It's a grid BFS with a twist. If you've got an OA invite, expect to build distances first and count treasures second. StealthCoder sits invisibly as a safety net on the live OA if you blank on the second half.

The problem

An adventurer starts at (0, 0) in a haunted castle and moves orthogonally around walls. Treasure cells may be collected at most once.
Return [minimumMoves, maximumTreasures], where the second value is the greatest number of distinct treasures collectible on any minimum-move route to escapePoint. Return [-1, 0] when escape is impossible.

Function
escapeCastleWithTreasures(rows: int, columns: int, walls: int[][], escapePoint: int[], treasures: int[][]) → int[]

Examples
Example 1
rows = 3
columns = 3
walls = [[1,1]]
escapePoint = [2,2]
treasures = [[0,1]]
return = [4,1]
A four-step route across the top collects the treasure before reaching the exit.
Example 2
rows = 2
columns = 2
walls = [[0,1],[1,0]]
escapePoint = [1,1]
treasures = []
return = [-1,0]
The start is sealed.
Example 3
rows = 2
columns = 3
walls = []
escapePoint = [0,2]
treasures = [[1,0],[1,1],[1,2]]
return = [2,0]
Collecting a treasure would require extra moves, so no treasure belongs to a shortest route.

Constraints
1 <= rows, columns <= 10.
0 <= treasures.length <= 15.
Walls, treasure cells, and the escape point use valid coordinates; the start and exit are not walls.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that treasures only count on shortest routes, so you never need a bitmask over all 15 treasures with detours. Run BFS from (0,0) to get dist1 for every cell, and BFS from the escape point to get dist2. A cell lies on some shortest path only if dist1 + dist2 equals the minimum. Then do a DP over those cells in order of dist1, carrying the max treasures collected so far. Each step moves from a cell to a neighbor with dist1 + 1 on the shortest-path set, and you add 1 when the neighbor is a treasure. Since dist1 strictly increases, no cell repeats, so each treasure is counted once automatically. The common pitfall is running a full state search with a visited mask, which is overkill and easy to get wrong. Also handle unreachable exits by returning [-1, 0]. If the layered DP slips your mind mid-assessment, StealthCoder is the hedge that surfaces it.

The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.

If this hits your live OA

You can drill Escape the Haunted Castle with Treasures 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. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Persona's OA.

Persona reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Escape the Haunted Castle with Treasures FAQ

What's the core trick in the Persona haunted castle problem?+

Only shortest routes matter. Compute BFS distances from the start and from the exit, keep cells where the two distances sum to the minimum, then run a DP across those cells by distance layer to maximize treasures collected.

Do I need a bitmask DP for the 15 treasures?+

No. The 15 limit is a decoy. Because routes must be minimum length, you never revisit cells, so treasures can't be double counted. A layered DP over the shortest-path cells is simpler and faster than a mask.

How hard is this one really?+

Medium. BFS on a 10 by 10 grid is easy. The second half, maximizing treasures only along shortest paths, is where people stall. Once you see the two-BFS filter plus layered DP, the code is short.

What edge cases should I test?+

Test a sealed start returning [-1, 0], an empty treasure list, a treasure off every shortest route like Example 3, and the start equal to the escape point. Also check that a treasure at the start or exit cell gets counted correctly.

How do I prepare for this in 48 hours?+

Practice multi-source and reverse BFS on grids, then the shortest-path DAG idea: filter by dist1 + dist2, then DP in distance order. Write it once from scratch so the treasure counting step is automatic.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Persona.

OA at Persona?
Invisible during screen share
Get it