Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start the backtracking with an empty combination.
DSA Python
def combination_sum_k(n, k): result = [] def backtrack(start, path, target): if target == 0: result.append(path[:]) return for i in range(start, n + 1): if i > target: break path.append(i) backtrack(i + 1, path, target - i) path.pop() backtrack(1, [1], k) return result
Attempts:
3 left
💡 Hint
Common Mistakes
Starting with a non-empty list causes wrong combinations.
Passing None or a number instead of a list causes errors.
✗ Incorrect
We start with an empty list to build combinations.
2fill in blank
mediumComplete the code to stop exploring when the current number exceeds the remaining target.
DSA Python
def combination_sum_k(n, k): result = [] def backtrack(start, path, target): if target == 0: result.append(path[:]) return for i in range(start, n + 1): if [1]: break path.append(i) backtrack(i + 1, path, target - i) path.pop() backtrack(1, [], k) return result
Attempts:
3 left
💡 Hint
Common Mistakes
Using i < target causes skipping valid numbers.
Checking target < 0 is unnecessary here.
✗ Incorrect
If the current number is greater than the target, no need to continue.
3fill in blank
hardFix the error in the recursive call to correctly reduce the target.
DSA Python
def combination_sum_k(n, k): result = [] def backtrack(start, path, target): if target == 0: result.append(path[:]) return for i in range(start, n + 1): if i > target: break path.append(i) backtrack(i + 1, path, [1]) path.pop() backtrack(1, [], k) return result
Attempts:
3 left
💡 Hint
Common Mistakes
Adding instead of subtracting increases target incorrectly.
Multiplying or dividing target causes wrong calculations.
✗ Incorrect
We subtract the chosen number from the target to track remaining sum.
4fill in blank
hardFill both blanks to create a dictionary comprehension that maps numbers to their squares for numbers less than 5.
DSA Python
squares = [1]: [2] for x in range(1, 10) if x < 5
Attempts:
3 left
💡 Hint
Common Mistakes
Using x*2 instead of x**2 gives wrong values.
Swapping keys and values reverses the dictionary.
✗ Incorrect
The keys are numbers x and values are squares x**2 for x less than 5.
5fill in blank
hardFill all three blanks to create a list comprehension that filters even numbers and squares them.
DSA Python
[x[1]2 for x in range(1, 11) if x [2] 2 == 0 and x > [3]]
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of ** for squaring.
Using < instead of > for filtering.
✗ Incorrect
We square x with **2, check even with %2, and filter x > 5.
