Bird
Raised Fist0
DSA Pythonprogramming~10 mins

Generate All Permutations of Array in DSA Python - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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]
Drag options to blanks, or click blank then click option'
Alen(nums)
Bi + 1
Cstart
Dstart + 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i + 1' instead of 'start + 1' causes skipping elements.
Using 'start' again causes infinite recursion.
2fill in blank
medium

Complete 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]
Drag options to blanks, or click blank then click option'
Astart
Blen(nums)
Cnums
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Printing 'start' or 'i' instead of the list.
Printing 'len(nums)' which is just the length.
3fill in blank
hard

Fix 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]
Drag options to blanks, or click blank then click option'
A== len(nums) - 1
B< len(nums) - 1
C> len(nums)
D>= len(nums)
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>= len(nums)' causes index errors.
Using '< len(nums) - 1' never reaches base case.
4fill in blank
hard

Fill 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]
Drag options to blanks, or click blank then click option'
A[:]
Bcopy()
Cresult
Dnums
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.
5fill in blank
hard

Fill 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]
Drag options to blanks, or click blank then click option'
Alen(nums) - 1
Blen(nums)
Cresult
Dstart
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.

Practice

(1/5)
1. What does the function permutations from Python's itertools module do when given an array as input?
easy
A. Generates all possible orderings of the elements in the array
B. Sorts the array in ascending order
C. Removes duplicate elements from the array
D. Returns the array reversed

Solution

  1. Step 1: Understand the purpose of permutations

    The permutations function creates all possible ways to arrange the elements of the input array.
  2. Step 2: Compare with other options

    Sorting, removing duplicates, or reversing are different operations and not what permutations does.
  3. Final Answer:

    Generates all possible orderings of the elements in the array -> Option A
  4. Quick Check:

    Permutations = all orderings [OK]
Hint: Permutations = all ways to arrange elements [OK]
Common Mistakes:
  • Confusing permutations with sorting
  • Thinking permutations remove duplicates
  • Assuming permutations reverse the array
2. Which of the following is the correct syntax to generate all permutations of the list [1, 2, 3] using itertools.permutations in Python?
easy
A. from itertools import permutation perms = permutations([1, 2, 3])
B. from itertools import permutation perms = permutation([1, 2, 3])
C. import itertools perms = itertools.permutations([1, 2, 3])
D. import itertools perms = itertools.permutation([1, 2, 3])

Solution

  1. Step 1: Check correct import and function name

    The correct module is itertools and the function is permutations (plural).
  2. Step 2: Verify syntax correctness

    import itertools perms = itertools.permutations([1, 2, 3]) correctly imports itertools and calls itertools.permutations with the list.
  3. Final Answer:

    import itertools perms = itertools.permutations([1, 2, 3]) -> Option C
  4. Quick Check:

    Correct import and function name = import itertools perms = itertools.permutations([1, 2, 3]) [OK]
Hint: Use plural 'permutations' and correct import [OK]
Common Mistakes:
  • Using singular 'permutation' instead of 'permutations'
  • Wrong import statement
  • Calling function without module prefix when not imported directly
3. What is the output of the following code?
from itertools import permutations
for p in permutations([1, 2]):
    print(p)
medium
A. (1, 2)\n(2, 1)
B. [1, 2]\n[2, 1]
C. (1, 2)\n(1, 2)
D. [1, 2]\n[1, 2]

Solution

  1. Step 1: Understand permutations output type

    itertools.permutations returns tuples representing each permutation.
  2. Step 2: List all permutations of [1, 2]

    They are (1, 2) and (2, 1), printed on separate lines.
  3. Final Answer:

    (1, 2)\n(2, 1) -> Option A
  4. Quick Check:

    Permutations of two elements = two tuples [OK]
Hint: Permutations output tuples, not lists [OK]
Common Mistakes:
  • Expecting lists instead of tuples
  • Repeating same permutation twice
  • Confusing output format with string or list
4. The following code is intended to print all permutations of [1, 2, 3]. What is the error?
from itertools import permutations
perms = permutations([1, 2, 3])
print(perms)
medium
A. The import statement is incorrect
B. It prints a permutations object, not the permutations themselves
C. The list should be converted to a tuple first
D. The function permutations does not exist

Solution

  1. Step 1: Understand what permutations returns

    permutations returns an iterator object, not a list of permutations directly.
  2. Step 2: Printing the iterator shows its object info, not permutations

    To see permutations, you must iterate or convert to list first.
  3. Final Answer:

    It prints a permutations object, not the permutations themselves -> Option B
  4. Quick Check:

    Printing iterator shows object info, not data [OK]
Hint: Convert iterator to list to see permutations [OK]
Common Mistakes:
  • Expecting print to show permutations directly
  • Thinking import is wrong
  • Believing input must be tuple
5. You want to generate all permutations of the array [1, 2, 2] but only keep unique permutations (no duplicates). Which approach correctly achieves this in Python?
hard
A. Use list(permutations([1, 2, 2])) directly
B. Use permutations(set([1, 2, 2])) to generate permutations of unique elements
C. Sort the array first, then use permutations
D. Use set(permutations([1, 2, 2])) to remove duplicate tuples

Solution

  1. Step 1: Understand duplicates in permutations

    Input has repeated elements, so permutations include duplicates.
  2. Step 2: Remove duplicates by converting permutations to a set

    Using set() on permutations removes duplicate tuples, keeping unique permutations only.
  3. Final Answer:

    Use set(permutations([1, 2, 2])) to remove duplicate tuples -> Option D
  4. Quick Check:

    Set removes duplicates from permutations [OK]
Hint: Convert permutations to set to remove duplicates [OK]
Common Mistakes:
  • Using set on input list before permutations (loses duplicates)
  • Assuming sorting removes duplicates
  • Not removing duplicates at all
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard