Bird
Raised Fist0
DSA Pythonprogramming~20 mins

Generate All Permutations of Array in DSA Python - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Permutation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Permutations for [1, 2]
What is the output of the following Python code that generates all permutations of the array [1, 2]?
DSA Python
from itertools import permutations
arr = [1, 2]
result = list(permutations(arr))
print(result)
A[(2, 1)]
B[(2, 1), (1, 2), (1, 2)]
C[(1, 2)]
D[(1, 2), (2, 1)]
Attempts:
2 left
💡 Hint
Think about how many ways you can arrange two distinct numbers.
❓ Predict Output
intermediate
2:00remaining
Number of Permutations for [1, 2, 3]
What is the length of the list generated by all permutations of the array [1, 2, 3] using itertools.permutations?
DSA Python
from itertools import permutations
arr = [1, 2, 3]
result = list(permutations(arr))
print(len(result))
A6
B3
C9
D5
Attempts:
2 left
💡 Hint
Number of permutations of n distinct elements is n factorial (n!).
🔧 Debug
advanced
3:00remaining
Identify the Error in Permutation Generation Code
What error will this code produce when trying to generate all permutations of [1, 2, 3]?
DSA Python
def permute(arr):
    if len(arr) == 1:
        return [arr]
    result = []
    for i in range(len(arr)):
        for p in permute(arr):
            if i != p:
                result.append([arr[i]] + p)
    return result

print(permute([1, 2, 3]))
ARecursionError due to infinite recursion
BTypeError because of wrong list concatenation
CNameError due to undefined variable
DNo error, prints all permutations
Attempts:
2 left
💡 Hint
Look at the recursive call inside the loop and what argument it uses.
❓ Predict Output
advanced
2:30remaining
Output of Custom Permutation Function
What is the output of this custom permutation function for input [1, 2]?
DSA Python
def permute(arr):
    if len(arr) == 0:
        return [[]]
    res = []
    for i in range(len(arr)):
        rest = arr[:i] + arr[i+1:]
        for p in permute(rest):
            res.append([arr[i]] + p)
    return res

print(permute([1, 2]))
A[[1], [2]]
B[[1, 2], [2, 1]]
C[[2, 1]]
D[[1, 2]]
Attempts:
2 left
💡 Hint
Think about how the function builds permutations by choosing each element and permuting the rest.
🧠 Conceptual
expert
2:00remaining
Time Complexity of Generating All Permutations
What is the time complexity of generating all permutations of an array of size n?
AO(2^n) because each element can be included or excluded
BO(n^2) because permutations are generated by nested loops
CO(n!) because there are n! permutations and each takes O(n) to build
DO(n) because each element is processed once
Attempts:
2 left
💡 Hint
Consider how many permutations exist and how much work is done per permutation.

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