0
0
DSA Cprogramming~20 mins

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

Choose your learning style9 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.