0
0
DSA Cprogramming~20 mins

Memoization to Optimize Recursion in DSA C - Practice Problems & Challenges

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

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

int main() {
    int n = 5;
    int memo[6];
    for (int i = 0; i <= n; i++) memo[i] = -1;
    int result = fib(n, memo);
    printf("%d\n", result);
    return 0;
}
A5
B8
C13
D3
Attempts:
2 left
💡 Hint
Recall that Fibonacci sequence starts with 0, 1, 1, 2, 3, 5...
🧠 Conceptual
intermediate
1:30remaining
Memoization Purpose in Recursion
What is the main purpose of using memoization in recursive functions?
ATo store intermediate results and avoid repeated calculations
BTo make the code shorter and easier to read
CTo increase the number of recursive calls
DTo convert recursion into iteration automatically
Attempts:
2 left
💡 Hint
Think about how recursion can repeat the same work multiple times.
🔧 Debug
advanced
2:00remaining
Identify the Error in Memoized Fibonacci
What error will occur when running this code snippet?
DSA C
#include <stdio.h>

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

int main() {
    int n = 5;
    int memo[6] = {0};
    int result = fib(n, memo);
    printf("%d\n", result);
    return 0;
}
AThe code prints 0 because memo array is initialized with zeros
BThe code runs correctly and prints 5
CThe code causes a segmentation fault
DThe code prints 1 instead of 5
Attempts:
2 left
💡 Hint
Check how memo array is initialized and how memo[n] is checked before recursion.
Predict Output
advanced
2:00remaining
Output of Factorial with Memoization
What is the output of this C program that uses memoization to compute factorial?
DSA C
#include <stdio.h>

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

int main() {
    int n = 4;
    int memo[5] = {0};
    int result = fact(n, memo);
    printf("%d\n", result);
    return 0;
}
A120
B10
C0
D24
Attempts:
2 left
💡 Hint
Recall factorial 4! = 4*3*2*1
🚀 Application
expert
2:30remaining
Memoization Impact on Time Complexity
Consider a naive recursive function to compute Fibonacci numbers with exponential time complexity. After adding memoization, what is the new time complexity?
AO(2^n)
BO(n^2)
CO(n)
DO(log n)
Attempts:
2 left
💡 Hint
Memoization avoids repeated calls by storing results.