Bird
Raised Fist0
DSA Cprogramming~20 mins

Unique Paths in Grid DP in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Unique Paths Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Unique Paths Calculation in 3x3 Grid
What is the output of the following C code that calculates the number of unique paths in a 3x3 grid moving only right or down?
DSA C
#include <stdio.h>

int uniquePaths(int m, int n) {
    int dp[m][n];
    for (int i = 0; i < m; i++) {
        dp[i][0] = 1;
    }
    for (int j = 0; j < n; j++) {
        dp[0][j] = 1;
    }
    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            dp[i][j] = dp[i-1][j] + dp[i][j-1];
        }
    }
    return dp[m-1][n-1];
}

int main() {
    printf("%d\n", uniquePaths(3, 3));
    return 0;
}
A9
B3
C6
D12
Attempts:
2 left
💡 Hint
Think about how many ways you can move right and down to reach the bottom-right corner.
🧠 Conceptual
intermediate
1:30remaining
Number of Unique Paths in a 2x4 Grid
How many unique paths exist in a 2 rows by 4 columns grid if you can only move right or down?
A6
B5
C7
D4
Attempts:
2 left
💡 Hint
Use the formula for combinations or think about the dp approach.
❓ Predict Output
advanced
2:30remaining
Output of Unique Paths with Obstacle Grid
What is the output of this C code that calculates unique paths in a 3x3 grid with an obstacle at position (1,1)? Obstacle cells are marked with 1 and cannot be passed.
DSA C
#include <stdio.h>

int uniquePathsWithObstacles(int obstacleGrid[3][3]) {
    int dp[3][3];
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) {
            dp[i][j] = 0;
        }
    }
    dp[0][0] = obstacleGrid[0][0] == 1 ? 0 : 1;
    for (int i = 1; i < 3; i++) {
        dp[i][0] = (obstacleGrid[i][0] == 0 && dp[i-1][0] == 1) ? 1 : 0;
    }
    for (int j = 1; j < 3; j++) {
        dp[0][j] = (obstacleGrid[0][j] == 0 && dp[0][j-1] == 1) ? 1 : 0;
    }
    for (int i = 1; i < 3; i++) {
        for (int j = 1; j < 3; j++) {
            if (obstacleGrid[i][j] == 0) {
                dp[i][j] = dp[i-1][j] + dp[i][j-1];
            } else {
                dp[i][j] = 0;
            }
        }
    }
    return dp[2][2];
}

int main() {
    int grid[3][3] = {
        {0, 0, 0},
        {0, 1, 0},
        {0, 0, 0}
    };
    printf("%d\n", uniquePathsWithObstacles(grid));
    return 0;
}
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
The obstacle blocks some paths, reducing total unique paths.
🔧 Debug
advanced
2:00remaining
Identify the Error in Unique Paths DP Code
What error will this C code produce when calculating unique paths in a 3x3 grid?
DSA C
#include <stdio.h>

int uniquePaths(int m, int n) {
    int dp[m][n];
    for (int i = 0; i <= m; i++) {
        dp[i][0] = 1;
    }
    for (int j = 0; j <= n; j++) {
        dp[0][j] = 1;
    }
    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            dp[i][j] = dp[i-1][j] + dp[i][j-1];
        }
    }
    return dp[m-1][n-1];
}

int main() {
    printf("%d\n", uniquePaths(3, 3));
    return 0;
}
ASegmentation fault due to out-of-bounds array access
BCompilation error due to missing return statement
COutput is 0
DNo error, output is 6
Attempts:
2 left
💡 Hint
Check the loop conditions for array indexing.
🚀 Application
expert
3:00remaining
Calculate Unique Paths in a 5x5 Grid with Obstacles
Given the following 5x5 grid with obstacles (1 marks obstacle, 0 free cell), how many unique paths exist from top-left to bottom-right moving only right or down?
DSA C
int grid[5][5] = {
    {0, 0, 0, 0, 0},
    {0, 1, 0, 1, 0},
    {0, 0, 0, 0, 0},
    {1, 0, 1, 0, 0},
    {0, 0, 0, 1, 0}
};
A10
B5
C8
D7
Attempts:
2 left
💡 Hint
Use dynamic programming to accumulate paths avoiding obstacles.

Practice

(1/5)
1. What does the problem "Unique Paths in Grid DP" ask you to find?
easy
A. The maximum sum path from the top-left to the bottom-right corner of a grid.
B. The shortest path length from the top-left to the bottom-right corner of a grid.
C. The number of obstacles in the grid.
D. The number of different ways to move from the top-left to the bottom-right corner of a grid using only right and down moves.

Solution

  1. Step 1: Understand the problem goal

    The problem asks for counting all possible paths from the start (top-left) to the end (bottom-right) moving only right or down.
  2. Step 2: Clarify movement constraints

    Only moves allowed are right or down, no diagonal or backward moves.
  3. Final Answer:

    The number of different ways to move from the top-left to the bottom-right corner of a grid using only right and down moves. -> Option D
  4. Quick Check:

    Unique paths = count of right/down moves combinations [OK]
Hint: Count paths by moving only right or down [OK]
Common Mistakes:
  • Thinking diagonal moves are allowed
  • Confusing path count with shortest path
  • Counting obstacles instead of paths
2. Which of the following is the correct way to initialize the DP array for a grid of size m x n in C?
easy
A. int dp[m][n] = {0};
B. int dp[m][n]; dp = 0;
C. int dp[m][n]; memset(dp, 0, sizeof(dp));
D. int dp[m][n] = NULL;

Solution

  1. Step 1: Understand array initialization in C

    Arrays cannot be assigned directly after declaration; memset is used to set all bytes to zero.
  2. Step 2: Check each option

    int dp[m][n] = {0}; tries to initialize with {0} which works only for static arrays, not variable length arrays. int dp[m][n]; memset(dp, 0, sizeof(dp)); uses memset correctly. int dp[m][n]; dp = 0; tries invalid assignment. int dp[m][n] = NULL; assigns NULL which is invalid for arrays.
  3. Final Answer:

    int dp[m][n]; memset(dp, 0, sizeof(dp)); -> Option C
  4. Quick Check:

    Use memset to zero initialize arrays in C [OK]
Hint: Use memset to zero initialize arrays in C [OK]
Common Mistakes:
  • Trying to assign 0 directly to array
  • Using NULL for arrays
  • Assuming {0} works for variable length arrays
3. What is the output of the following code snippet for m=3, n=3?
int uniquePaths(int m, int n) {
    int dp[m][n];
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < m; i++) dp[i][0] = 1;
    for (int j = 0; j < n; j++) dp[0][j] = 1;
    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            dp[i][j] = dp[i-1][j] + dp[i][j-1];
        }
    }
    return dp[m-1][n-1];
}
printf("%d", uniquePaths(3, 3));
medium
A. 3
B. 6
C. 9
D. 1

Solution

  1. Step 1: Initialize first row and column

    First row and column are set to 1 because only one way to reach cells in first row (all right moves) or first column (all down moves).
  2. Step 2: Fill dp table using sum of top and left cells

    For each cell dp[i][j], value = dp[i-1][j] + dp[i][j-1]. For 3x3 grid, dp[2][2] = 6.
  3. Final Answer:

    6 -> Option B
  4. Quick Check:

    Unique paths in 3x3 grid = 6 [OK]
Hint: Sum paths from top and left cells to fill DP [OK]
Common Mistakes:
  • Not initializing first row and column to 1
  • Adding instead of summing dp[i-1][j] and dp[i][j-1]
  • Returning wrong dp cell
4. Find the error in this code snippet for unique paths calculation:
int uniquePaths(int m, int n) {
    int dp[m][n];
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i <= m; i++) dp[i][0] = 1;
    for (int j = 0; j <= n; j++) dp[0][j] = 1;
    for (int i = 1; i < m; i++) {
        for (int j = 1; j < n; j++) {
            dp[i][j] = dp[i-1][j] + dp[i][j-1];
        }
    }
    return dp[m-1][n-1];
}
medium
A. The loops initializing dp go out of bounds causing runtime error.
B. The dp array is not zero initialized.
C. The return statement returns wrong cell.
D. The nested loops start from 0 instead of 1.

Solution

  1. Step 1: Check loop bounds for initialization

    Loops use <= m and <= n which causes index to go out of bounds since valid indices are 0 to m-1 and 0 to n-1.
  2. Step 2: Identify correct loop bounds

    Loops should use < m and < n to avoid accessing invalid indices.
  3. Final Answer:

    The loops initializing dp go out of bounds causing runtime error. -> Option A
  4. Quick Check:

    Array indices must be within 0 to size-1 [OK]
Hint: Use < not <= for array indices in loops [OK]
Common Mistakes:
  • Using <= instead of < in loops
  • Ignoring array bounds causing crashes
  • Incorrect initialization of dp array
5. Given a 4x4 grid with some blocked cells (obstacles) represented by 1, how would you modify the unique paths DP solution to count paths avoiding obstacles? Grid:
0 0 0 0
0 1 0 0
0 0 1 0
0 0 0 0
hard
A. Set dp[i][j] = 0 if cell is blocked; else dp[i][j] = dp[i-1][j] + dp[i][j-1].
B. Ignore obstacles and calculate paths normally.
C. Set dp[i][j] = 1 for blocked cells and 0 otherwise.
D. Only count paths that move diagonally to avoid obstacles.

Solution

  1. Step 1: Mark blocked cells in dp as zero

    If a cell is blocked (value 1), no paths can go through it, so dp[i][j] = 0.
  2. Step 2: Calculate dp for free cells

    For free cells (value 0), dp[i][j] = dp[i-1][j] + dp[i][j-1], summing paths from top and left.
  3. Final Answer:

    Set dp[i][j] = 0 if cell is blocked; else dp[i][j] = dp[i-1][j] + dp[i][j-1]. -> Option A
  4. Quick Check:

    Blocked cells have zero paths [OK]
Hint: Zero dp for obstacles, sum paths for free cells [OK]
Common Mistakes:
  • Ignoring obstacles in path count
  • Assigning 1 to blocked cells
  • Using diagonal moves to avoid obstacles
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard