DSA interview question
You're building a warehouse robot navigation simulator. A robot starts at the top-left corner of an m x n warehouse grid and needs to reach the bottom-right corner to pick up a package. The robot can only move right or down at each step. Some cells contain shelving units (obstacles) that the robot cannot pass through. Your task: compute the number of distinct paths the robot can take from start to destination, avoiding all obstacles.
Example: grid
0 0 0
0 1 0
0 0 0
(1 = obstacle, 0 = free cell) → there are 2 unique paths avoiding the obstacle in the middle.
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid);
Input obstacleGrid | Output | Explanation |
|---|---|---|
[[0,0,0],[0,1,0],[0,0,0]] | 2 | Two paths around the middle obstacle |
[[0,1],[0,0]] | 1 | Only one path: down, down, right (obstacle blocks top-right) |
[[1,0]] | 0 | Start cell itself is an obstacle → no paths possible |
[[0]] | 1 | Single free cell, start = end, one trivial path |
[[1]] | 0 | Single cell that is an obstacle → no paths |
[[0,0],[1,1],[0,0]] | 0 | Entire middle row blocked → impossible to reach bottom |
[[0,0,0]] | 1 | Single row, no obstacles, one straight path |
(Confirm: grid contains only 0s and 1s; if the start or end cell is itself an obstacle, answer is 0.)
1 an obstacle and 0 a free cell, or could it be reversed? Confirm the convention.long long)?m and n guaranteed to be at least 1, i.e., is an empty grid a possibility I need to handle?On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in