Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the memoization map for Fibonacci.
DSA Go
memo := make(map[int]int)
result := fib([1], memo)Attempts:
3 left
💡 Hint
Common Mistakes
Passing 0 or 1 as input which returns base cases immediately.
Passing variable 'n' without defining it.
✗ Incorrect
We call fib with 10 to compute the 10th Fibonacci number using memoization.
2fill in blank
mediumComplete the code to check if the Fibonacci value is already computed in memo.
DSA Go
if val, ok := memo[[1]]; ok { return val }
Attempts:
3 left
💡 Hint
Common Mistakes
Using memo as key instead of n.
Using result or fib which are not keys.
✗ Incorrect
We check if the current input n is already in the memo map to avoid recomputation.
3fill in blank
hardFix the error in the recursive Fibonacci call to use memoization correctly.
DSA Go
memo[[1]] = fib(n-1, memo) + fib(n-2, memo)
Attempts:
3 left
💡 Hint
Common Mistakes
Using memo or val as keys instead of n.
Not storing the result in memo at all.
✗ Incorrect
We store the computed Fibonacci value for n in memo[n] to reuse later.
4fill in blank
hardFill both blanks to complete the recursive Fibonacci function with memoization.
DSA Go
func fib(n int, memo map[int]int) int {
if n <= 1 {
return n
}
if val, ok := memo[[1]]; ok {
return val
}
memo[[2]] = fib(n-1, memo) + fib(n-2, memo)
return memo[n]
}Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys for lookup and assignment.
Using variables like val or memo as keys.
✗ Incorrect
Both blanks use n as the key to check and store Fibonacci values in memo.
5fill in blank
hardFill all three blanks to create a map comprehension that filters Fibonacci numbers greater than 5.
DSA Go
filtered := map[int]int{}
for k, v := range memo {
if v [1] 5 {
filtered[[2]] = [3]
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > in condition.
Swapping key and value in assignment.
✗ Incorrect
We filter memo to keep entries where value v is greater than 5, copying key k and value v.
