Problem · Matrix

Enumerate Top-to-Bottom Grid Paths

HardGoogleFULLTIMEONSITE INTERVIEW
See Google hiring insights

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.

A path may start at any open cell in the top row. At each step, it may move one cell down, right, left, or up, staying inside the matrix and entering only open cells. A path is simple: it may not visit the same cell more than once. The path ends immediately when it first reaches the bottom row.

The problem statement continues
Pro

Examples

Example 1

grid = [[0,0],[0,0]]return = [[[0,0],[1,0]],[[0,0],[0,1],[1,1]],[[0,1],[1,1]],[[0,1],[0,0],[1,0]]]

Both top-row cells are valid starts. Depth-first search uses down, right, left, then up, and stops each path as soon as it reaches row 1.

FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$9/month

Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week
CodePython 3
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 Google interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 2 more worked examples, explained
  • Guided hints and editorial
  • Run your code on real test cases
$9/month

Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week