Challenge - 5 Problems
Permutation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput 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)
Attempts:
2 left
💡 Hint
Think about how many ways you can arrange two distinct numbers.
✗ Incorrect
The permutations of [1, 2] are two: (1, 2) and (2, 1). itertools.permutations returns tuples of all possible orderings.
❓ Predict Output
intermediateNumber 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))
Attempts:
2 left
💡 Hint
Number of permutations of n distinct elements is n factorial (n!).
✗ Incorrect
For 3 elements, the number of permutations is 3! = 3 × 2 × 1 = 6.
🔧 Debug
advancedIdentify 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]))
Attempts:
2 left
💡 Hint
Look at the recursive call inside the loop and what argument it uses.
✗ Incorrect
The function calls permute(arr) inside the loop without reducing the input size, causing infinite recursion and eventually a RecursionError.
❓ Predict Output
advancedOutput 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]))
Attempts:
2 left
💡 Hint
Think about how the function builds permutations by choosing each element and permuting the rest.
✗ Incorrect
The function correctly generates all permutations by recursively removing one element and permuting the rest, then adding the removed element at the front.
🧠 Conceptual
expertTime Complexity of Generating All Permutations
What is the time complexity of generating all permutations of an array of size n?
Attempts:
2 left
💡 Hint
Consider how many permutations exist and how much work is done per permutation.
✗ Incorrect
There are n! permutations of n elements. Generating each permutation requires copying or building a list of length n, so total time is O(n × n!) = O(n!).
