0
0
DSA Cprogramming~20 mins

Memoization Top Down DP in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Memoization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Memoized Fibonacci Function
What is the output of the following C code that uses memoization to compute Fibonacci numbers?
DSA C
#include <stdio.h>
#define MAX 10

int memo[MAX];

int fib(int n) {
    if (n <= 1) return n;
    if (memo[n] != -1) return memo[n];
    memo[n] = fib(n-1) + fib(n-2);
    return memo[n];
}

int main() {
    for (int i = 0; i < MAX; i++) memo[i] = -1;
    printf("%d\n", fib(7));
    return 0;
}
A8
B21
C13
D34
Attempts:
2 left
💡 Hint
Recall the Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34...
Predict Output
intermediate
2:00remaining
Memoized Factorial Computation Output
What is the output of this C code that uses memoization to compute factorials?
DSA C
#include <stdio.h>
#define MAX 6

int memo[MAX];

int fact(int n) {
    if (n <= 1) return 1;
    if (memo[n] != -1) return memo[n];
    memo[n] = n * fact(n-1);
    return memo[n];
}

int main() {
    for (int i = 0; i < MAX; i++) memo[i] = -1;
    printf("%d\n", fact(5));
    return 0;
}
A720
B120
C24
D60
Attempts:
2 left
💡 Hint
Recall factorial of 5 is 5*4*3*2*1.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Memoized Climbing Stairs Code
What error does the following C code produce when run, and why?
DSA C
#include <stdio.h>
#define MAX 6

int memo[MAX];

int climb(int n) {
    if (n <= 1) return 1;
    if (memo[n] != -1) return memo[n];
    memo[n] = climb(n-1) + climb(n-2);
    return memo[n];
}

int main() {
    for (int i = 0; i < MAX; i++) memo[i] = -1;
    printf("%d\n", climb(5));
    return 0;
}
ASegmentation fault due to accessing memo[5] out of bounds
BPrints 5 correctly
CCompilation error due to missing return type
DInfinite recursion causing stack overflow
Attempts:
2 left
💡 Hint
Check array size and index usage carefully.
🧠 Conceptual
advanced
2:00remaining
Memoization vs Tabulation in DP
Which statement correctly describes the difference between memoization and tabulation in dynamic programming?
AMemoization uses recursion with caching to avoid repeated work; tabulation solves subproblems iteratively in a bottom-up manner.
BMemoization solves subproblems bottom-up and stores results in a table; tabulation uses recursion with caching.
CMemoization and tabulation are identical techniques with different names.
DTabulation uses recursion and memoization uses iteration.
Attempts:
2 left
💡 Hint
Think about how recursion and iteration relate to these techniques.
🚀 Application
expert
3:00remaining
Memoized Edit Distance Computation Output
What is the output of the following C code that computes the edit distance between two strings using memoization?
DSA C
#include <stdio.h>
#include <string.h>
#define MAX 10

int memo[MAX][MAX];

int min(int a, int b, int c) {
    int m = a < b ? a : b;
    return m < c ? m : c;
}

int editDist(char *s1, char *s2, int m, int n) {
    if (m == 0) return n;
    if (n == 0) return m;
    if (memo[m][n] != -1) return memo[m][n];
    if (s1[m-1] == s2[n-1])
        memo[m][n] = editDist(s1, s2, m-1, n-1);
    else
        memo[m][n] = 1 + min(editDist(s1, s2, m, n-1), editDist(s1, s2, m-1, n), editDist(s1, s2, m-1, n-1));
    return memo[m][n];
}

int main() {
    char s1[] = "kitten";
    char s2[] = "sitting";
    for (int i = 0; i < MAX; i++)
        for (int j = 0; j < MAX; j++)
            memo[i][j] = -1;
    printf("%d\n", editDist(s1, s2, strlen(s1), strlen(s2)));
    return 0;
}
A2
B4
C5
D3
Attempts:
2 left
💡 Hint
Edit distance between 'kitten' and 'sitting' is a classic example.