Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to start generating permutations by swapping elements.
DSA Python
def permute(nums, start=0): if start == len(nums) - 1: print(nums) return for i in range(start, len(nums)): nums[start], nums[i] = nums[i], nums[start] permute(nums, [1]) nums[start], nums[i] = nums[i], nums[start]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i + 1' instead of 'start + 1' causes skipping elements.
Using 'start' again causes infinite recursion.
✗ Incorrect
We move to the next index by increasing 'start' by 1 to generate permutations correctly.
2fill in blank
mediumComplete the code to print all permutations of the list.
DSA Python
def permute(nums, start=0): if start == len(nums) - 1: print([1]) return for i in range(start, len(nums)): nums[start], nums[i] = nums[i], nums[start] permute(nums, start + 1) nums[start], nums[i] = nums[i], nums[start]
Attempts:
3 left
💡 Hint
Common Mistakes
Printing 'start' or 'i' instead of the list.
Printing 'len(nums)' which is just the length.
✗ Incorrect
We print the current permutation stored in 'nums' when we reach the last index.
3fill in blank
hardFix the error in the base case condition to correctly identify when to print a permutation.
DSA Python
def permute(nums, start=0): if start [1] len(nums): print(nums) return for i in range(start, len(nums)): nums[start], nums[i] = nums[i], nums[start] permute(nums, start + 1) nums[start], nums[i] = nums[i], nums[start]
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>= len(nums)' causes index errors.
Using '< len(nums) - 1' never reaches base case.
✗ Incorrect
The base case is when 'start' reaches the last index, which is length minus one.
4fill in blank
hardFill both blanks to create a function that returns all permutations as a list.
DSA Python
def permute(nums, start=0, result=None): if result is None: result = [] if start == len(nums) - 1: result.append(nums[1]) return for i in range(start, len(nums)): nums[start], nums[i] = nums[i], nums[start] permute(nums, start + 1, result) nums[start], nums[i] = nums[i], nums[start] return [2]
Attempts:
3 left
💡 Hint
Common Mistakes
Appending 'nums' directly causes all entries to be the same list.
Returning 'nums' instead of 'result' loses collected permutations.
✗ Incorrect
We append a copy of 'nums' using slicing to avoid reference issues, and return 'result' list.
5fill in blank
hardFill all three blanks to generate permutations using backtracking and return the result list.
DSA Python
def permute(nums): result = [] def backtrack(start=0): if start == [1]: result.append(nums[:]) return for i in range(start, [2]): nums[start], nums[i] = nums[i], nums[start] backtrack(start + 1) nums[start], nums[i] = nums[i], nums[start] backtrack() return [3]
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'len(nums) - 1' as base case causes missing last permutation.
Returning 'nums' instead of 'result' loses all permutations.
✗ Incorrect
The base case is when 'start' equals length of nums; loop runs to length; return collected 'result'.
