Out of Boundary Paths
A medium-tier problem at 48% community acceptance, tagged with Dynamic Programming. Reported in interviews at Baidu and 0 others.
Out of Boundary Paths is a medium-difficulty DP problem that tests your ability to count constrained paths in a grid. You're on an m by n grid with a ball at position (x, y), and you have N moves to make. Each move goes up, down, left, or right. The task: count how many distinct sequences of moves result in the ball leaving the grid. It's trickier than it looks because the naive recursive solution explodes exponentially. Baidu has asked this one. If you hit it live and your first instinct leads to timeout, StealthCoder solves it invisibly during your assessment.
Companies that ask "Out of Boundary Paths"
Out of Boundary Paths is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround.
Get StealthCoderThe trick is memoization. At each state (row, col, moves_left), you can either move out of bounds (success) or move to an adjacent cell and recurse. The brute-force tree is huge, but there are only m * n * (N+1) unique states, so DP collapses it. Most candidates try raw recursion without memoization and fail on larger inputs. The path-counting aspect also catches people who confuse this with shortest-path problems. You need to count ALL valid exit sequences, not find one optimal path. The modulo arithmetic (typically 10^9 + 7) is standard but easy to forget. If you blank on the state definition or how to structure the recursion during a live OA, StealthCoder runs silently on screen share and gives you a working template in seconds.
Pattern tags
You know the problem.
Make sure you actually pass it.
Out of Boundary Paths recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built because the OA filter rejects engineers who'd pass the on-site. That's a broken filter. This is the workaround. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Out of Boundary Paths interview FAQ
Is this really just memoization on top of DFS?+
Yes. The state is (row, col, moves_remaining). From each state, you try all four directions. If you step out of bounds, increment the count. If you're still in bounds, recurse with moves_remaining - 1. Memoize by state to avoid recomputing. Without memoization, the tree explodes. With it, you solve it in O(m * n * N) time.
Why is this asked at Baidu if it's just DP boilerplate?+
The state-space design isn't trivial for everyone. Many candidates conflate it with grid-path-sum or pathfinding problems. The counting aspect (not minimization) throws people off. It also forces you to handle modular arithmetic correctly. Companies use it to filter out candidates who panic on unfamiliar DP formulations.
What's the acceptance rate telling me about difficulty?+
48 percent acceptance is solid for a medium. Not as punishing as hard problems, but way more candidates fail than on easy ones. That gap usually means the first instinct is wrong or incomplete. You can't brute-force it. You need to recognize the DP structure quickly.
Do I need to optimize space, or is O(m * n * N) memoization fine?+
For an online assessment, O(m * n * N) space via a hashmap or 3D array is standard and almost always accepted. Space optimization (rolling a 2D DP table across moves) is nice but rarely required. Focus on correctness and modular arithmetic. If it passes, you're done.
How do I avoid off-by-one errors with the boundary checks?+
Think of it as: if you move and the new position is out of bounds (row < 0 or row >= m or col < 0 or col >= n), you've successfully exited. Increment the count and return. If still in bounds, recurse with moves_remaining - 1. Test with a small grid and N=1 or N=2 by hand first.
Want the actual problem statement? View "Out of Boundary Paths" on LeetCode →