Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Routes around blocked cells

    MediumAbout 25 minFull AccessLearn it: Unique Paths and Longest Common Subsequence: 2D state

    The task

    A delivery robot crosses a city grid moving only right or down, and some blocks are closed for roadworks.

    Complete CountPathsWithObstacles(int[][] grid). grid[r][c] is 0 for an open cell and 1 for a closed one; the grid has at least one row and one column, and all rows have the same length. Return how many routes go from the top-left cell to the bottom-right cell, moving only right or down and never entering a closed cell, as a long. A closed start or finish gives 0.

    Signature

    public static long CountPathsWithObstacles(int[][] grid)

    Examples

    • [[0, 1, 0], [0, 0, 0]] → 1. Down first, then right twice; the closed cell blocks the top row.
    • [[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]] → 4. The closed cell removes the routes that pass through it.

    Limits: Up to 30 rows and 30 columns; the count fits in a long.

    The tests check: one cell, a closed start, a closed finish, a closed cell in the only row, one column, a wall with one gap, and a 30 × 30 open grid in time, a count beyond int.

    Hints

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // The number of right-and-down routes from the top-left to the bottom-right cell that avoid closed cells (1).
          public static long CountPathsWithObstacles(int[][] grid)
          {
              throw new NotImplementedException();
          }
      }

      This problem comes with TechInterviewNotes Full Access

      Unlock all paid content on TechInterviewNotes.