Challenge - 5 Problems
Permutation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Recursive Permutation Generation
What is the output of this Rust code that generates all permutations of the array [1, 2]?
DSA Rust
fn permute(nums: &mut Vec<i32>, start: usize, result: &mut Vec<Vec<i32>>) {
if start == nums.len() {
result.push(nums.clone());
return;
}
for i in start..nums.len() {
nums.swap(start, i);
permute(nums, start + 1, result);
nums.swap(start, i);
}
}
fn main() {
let mut nums = vec![1, 2];
let mut result = Vec::new();
permute(&mut nums, 0, &mut result);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Think about how swapping elements and recursion generate all orderings.
✗ 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
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 Code
What error does this Rust code produce when trying to generate permutations of [1, 2, 3]?
fn permute(nums: &mut Vec, start: usize, result: &mut Vec>) {
if start == nums.len() {
result.push(nums.clone());
return;
}
for i in start..nums.len() {
nums.swap(start, i);
permute(nums, start + 1, result);
}
}
fn main() {
let mut nums = vec![1, 2, 3];
let mut result = Vec::new();
permute(&mut nums, 0, &mut result);
println!("{:?}", result);
}
Attempts:
2 left
💡 Hint
Check if the array is restored after recursive calls.
✗ Incorrect
The code swaps elements before recursion but does not swap back after recursion, so the array state is not restored, leading to incorrect permutations.
🚀 Application
advancedPermutations of Array with Duplicate Elements
Given the array [1, 1, 2], how many unique permutations are there?
Attempts:
2 left
💡 Hint
Consider that swapping identical elements does not create new unique permutations.
✗ Incorrect
The total permutations of 3 elements is 3! = 6, but since two elements are identical, divide by 2! to get unique permutations: 6 / 2 = 3. However, the unique permutations are [1,1,2], [1,2,1], and [2,1,1], so 3 unique permutations.
❓ Predict Output
expertOutput of Permutation Generation with Early Return
What is the output of this Rust code that attempts to generate all permutations of [1, 2, 3] but returns early after the first permutation?
DSA Rust
fn permute(nums: &mut Vec<i32>, start: usize, result: &mut Vec<Vec<i32>>) {
if start == nums.len() {
result.push(nums.clone());
return;
}
for i in start..nums.len() {
nums.swap(start, i);
permute(nums, start + 1, result);
return;
nums.swap(start, i);
}
}
fn main() {
let mut nums = vec![1, 2, 3];
let mut result = Vec::new();
permute(&mut nums, 0, &mut result);
println!("{:?}", result);
}Attempts:
2 left
💡 Hint
Look at the placement of the return statement inside the loop.
✗ Incorrect
The return inside the loop causes the function to stop after the first recursive call, so only the first permutation [1, 2, 3] is generated.
