Reported September 2020
Wells Fargobreadth first search

Matrix Path Existence

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

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

The mistake that sinks a first attempt on this Wells Fargo OA, reported in September 2020, is forgetting that the start cell can be blocked. Example 4 exists to punish exactly that. The task is simple on paper: given a binary grid, can you walk from the top-left to the bottom-right using up, down, left, right moves on cells marked 1? It's a flood-fill reachability problem, so BFS or DFS fits. Most people know that. The misses come from edge cases and visited handling. If you blank on the day, StealthCoder runs invisibly as a safety net on the live OA and gives you a working solution.

The problem

Given a rectangular binary matrix grid, determine whether a path exists from the top-left cell to the bottom-right cell.
A cell containing 1 is passable.
A cell containing 0 is blocked.
You may move one cell at a time up, down, left, or right.
Return true when such a path exists and false otherwise.

Function
pathExists(grid: int[][]) → boolean

Examples
Example 1
grid = [[1,1,0],[0,1,1],[0,0,1]]
return = true
A path follows the passable cells from the top-left cell through the center to the bottom-right cell.
Example 2
grid = [[1,0],[0,1]]
return = false
Diagonal movement is not allowed, so the two passable cells are disconnected.
Example 3
grid = [[1]]
return = true
The single cell is both the start and the destination.
Example 4
grid = [[0,1],[1,1]]
return = false
The starting cell is blocked.

Constraints
1 <= grid.length <= 200.
1 <= grid[i].length <= 200.
Every row has the same length.
grid[i][j] is 0 or 1.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is plain graph traversal on a grid. Check the start and end cells first. If grid[0][0] is 0 or the last cell is 0, return false immediately. Then run BFS with a queue from (0,0), marking cells visited the moment you push them, not when you pop them. Skipping that makes the queue balloon and can time out on a 200 by 200 grid. Four directions only, so no diagonals. Return true as soon as you pop or reach (rows-1, cols-1). The 1x1 grid of [[1]] returns true because start equals destination, so make sure your early checks don't break it. Recursive DFS works too, but 40,000 cells can overflow the stack in some languages, so prefer iterative BFS. If you freeze on the live OA, StealthCoder is the hedge that gets you unstuck. Complexity is O(rows*cols) time and space.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Matrix Path Existence 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 StealthCoder

Related leaked OAs

⏵ The honest play

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

Wells Fargo 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.

Matrix Path Existence FAQ

How hard is the Wells Fargo Matrix Path Existence problem really?+

Easy to medium. It's a standard grid reachability question. If you've written BFS on a grid once, you can finish it fast. The difficulty is in the edge cases: a blocked start cell, a blocked end cell, and the single-cell grid.

What's the trick to solving it?+

Treat the grid as a graph and run BFS from the top-left over cells equal to 1. Mark visited when you enqueue. Return true if you reach the bottom-right, false if the queue empties first. Check that the start cell is passable before anything else.

Should I use BFS or DFS here?+

Either gives the right answer, since you only need existence, not shortest path. BFS with an explicit queue is safer because a 200 by 200 grid can mean recursion depth around 40,000. Iterative DFS with a stack also works fine.

Which edge cases do people miss?+

The blocked start cell (Example 4), a blocked destination, and the 1x1 grid that should return true. Also watch bounds checks on both rows and columns, since the grid can be non-square, and never allow diagonal moves.

How do I prepare for this in 48 hours?+

Write grid BFS from scratch twice with a direction array and a visited matrix. Then test it on the four examples here. Also do a couple of flood-fill style variants so the bounds checking and visited logic become automatic.

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

OA at Wells Fargo?
Invisible during screen share
Get it