Binary Matrix Top-to-Bottom Reachability
Updated
Problem statement
You are given a rectangular binary matrix grid. A cell with value 0 is open, and a cell with value 1 is blocked.
You may start at any open cell in the top row. From an open cell, you may move one cell up, down, left, or right, staying inside the matrix and never entering a blocked cell.
Return true if at least one open cell in the bottom row is reachable from an open cell in the top row. Otherwise, return false.
Function
canReachBottom(grid: int[][]) → booleanExamples
Example 1
grid = [[0,1,1],[0,0,1],[1,0,0]]return = trueOne valid route is (0,0) -> (1,0) -> (1,1) -> (2,1). It reaches an open bottom-row cell.
Example 2
grid = [[0,1,0],[1,1,1],[0,0,0]]return = falseThe blocked middle row separates every open top-row cell from the bottom row.
Example 3
grid = [[1,0,1]]return = trueThe only row is both the top and bottom row. Its open cell is already a valid destination.
Constraints
1 <= grid.length <= 5001 <= grid[i].length <= 500- Every row has the same length.
- Every cell is either
0or1. - A move changes the row or column by exactly
1, but not both.
More Google Onsite Interview questions
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Number of IslandsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
- Mouse and Cheese ReachabilityMeta · ONSITE INTERVIEWMeta interview questions
- Number of IslandsTripjack · ONSITE INTERVIEWTripjack interview questions
- Number of IslandsTekion · ONSITE INTERVIEWTekion interview questions
- Grid Pathfinding with Obstacles (DFS)Roblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Number of IslandsGoldman Sachs · OA · PHONE SCREEN · ONSITE INTERVIEWGoldman Sachs interview questions
- Number of IslandsBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Number of IslandsCrowdstrike · ONSITE INTERVIEWCrowdstrike interview questions
- Max Area of IslandRoku · ONSITE INTERVIEWRoku interview questions