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 Array [1, 2]
What is the output of the following Java code that generates all permutations of the array [1, 2]?
DSA Java
import java.util.*; public class Permutations { public static void permute(int[] arr, int start, List<List<Integer>> result) { if (start == arr.length) { List<Integer> perm = new ArrayList<>(); for (int num : arr) perm.add(num); result.add(perm); return; } for (int i = start; i < arr.length; i++) { int temp = arr[start]; arr[start] = arr[i]; arr[i] = temp; permute(arr, start + 1, result); temp = arr[start]; arr[start] = arr[i]; arr[i] = temp; } } public static void main(String[] args) { int[] arr = {1, 2}; List<List<Integer>> result = new ArrayList<>(); permute(arr, 0, result); System.out.println(result); } }
Attempts:
2 left
💡 Hint
Think about how swapping elements and recursion generate all possible orders.
✗ Incorrect
The code swaps elements starting from the current index and recursively generates all permutations. For [1, 2], the permutations are [1, 2] and [2, 1].
🧠 Conceptual
intermediateNumber of Permutations for Array of Length 3
How many permutations are there for an array of length 3?
Attempts:
2 left
💡 Hint
Recall factorial definition: n! = n × (n-1) × ... × 1
✗ Incorrect
The number of permutations of an array of length n is n factorial (n!). For n=3, 3! = 3 × 2 × 1 = 6.
🔧 Debug
advancedIdentify the Error in Permutation Code
What error does the following Java code produce when generating permutations of [1, 2, 3]?
public static void permute(int[] arr, int start, List
- > result) {
if (start == arr.length - 1) {
List
Attempts:
2 left
💡 Hint
Check the base case condition carefully and how it affects the last recursion step.
✗ Incorrect
No error; the code runs correctly and generates all permutations. The base case `if (start == arr.length - 1)` works equivalently to `if (start == arr.length)` because when `start` reaches the last index, the permutation is complete and no further swaps are needed (the loop at that level would be a no-op).
🚀 Application
advancedPermutations of Array with Duplicate Elements
Given the array [1, 1, 2], which option shows the correct list of unique permutations generated by a proper algorithm?
Attempts:
2 left
💡 Hint
Duplicates cause repeated permutations; a good algorithm avoids duplicates in the output.
✗ Incorrect
The unique permutations of [1, 1, 2] are three: [1, 1, 2], [1, 2, 1], and [2, 1, 1]. Option C lists these without duplicates.
🧠 Conceptual
expertTime Complexity of Generating All Permutations
What is the time complexity of generating all permutations of an array of length n?
Attempts:
2 left
💡 Hint
Consider how many permutations exist and how much work is done per permutation.
✗ Incorrect
There are n! permutations of an array of length n. Generating each permutation requires at least O(n) work, so total complexity is O(n × n!) which is simplified as O(n!).
