Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to return the Fibonacci number for n using recursion.
DSA Python
def fib(n): if n <= 1: return n return fib(n-1) [1] fib(n-2)
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Using multiplication or division which is incorrect for Fibonacci.
✗ Incorrect
The Fibonacci number is the sum of the two previous Fibonacci numbers: fib(n-1) + fib(n-2).
2fill in blank
mediumComplete the code to store computed Fibonacci values in a dictionary to avoid repeated calculations.
DSA Python
def fib_memo(n, memo={}): if n in memo: return memo[n] if n <= 1: return n memo[n] = fib_memo(n-1, memo) [1] fib_memo(n-2, memo) return memo[n]
Attempts:
3 left
💡 Hint
Common Mistakes
Not adding the two recursive calls.
Forgetting to store the result in memo.
✗ Incorrect
We add the two previous Fibonacci numbers and store the result in memo to avoid repeated work.
3fill in blank
hardFix the error in the code to correctly implement the bottom-up dynamic programming approach for Fibonacci.
DSA Python
def fib_dp(n): dp = [0, 1] + [0] * (n-1) for i in range(2, n+1): dp[i] = dp[i-1] [1] dp[i-2] return dp[n]
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Incorrectly initializing the dp list size.
✗ Incorrect
Each Fibonacci number is the sum of the two previous numbers in the dp list.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps each number to its square only if the number is {{BLANK_1}} 5 and {{BLANK_2}} 10.
DSA Python
squares = {x: x**2 for x in range(1, 15) if x [1] 5 and x [2] 10}Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' or '<=' which includes boundary numbers.
Swapping the comparison operators.
✗ Incorrect
We want numbers greater than 5 and less than 10 to be included in the dictionary.
5fill in blank
hardFill all three blanks to create a dictionary comprehension that maps each word in words to its length only if the length is {{BLANK_1}} 3 and the word starts with {{BLANK_2}} 'a' or {{BLANK_3}} 'b'.
DSA Python
words = ['apple', 'bat', 'cat', 'ant', 'ball', 'dog'] result = {word: len(word) for word in words if len(word) [1] 3 and (word.startswith([2]) or word.startswith([3]))}
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' for length check.
Not quoting the letters in startswith.
✗ Incorrect
We select words with length exactly 3 and starting with 'a' or 'b'.
