Challenge - 5 Problems
Next Permutation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Next Permutation on a Simple Array
What is the output of the following Rust code that computes the next permutation of the array?
DSA Rust
fn next_permutation(nums: &mut [i32]) {
let n = nums.len();
if n < 2 { return; }
let mut i = n - 2;
while i != usize::MAX && nums[i] >= nums[i + 1] {
if i == 0 { break; }
i -= 1;
}
if nums[i] < nums[i + 1] {
let mut j = n - 1;
while nums[j] <= nums[i] {
j -= 1;
}
nums.swap(i, j);
}
nums[i + 1..].reverse();
}
fn main() {
let mut arr = vec![1, 2, 3];
next_permutation(&mut arr);
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
Think about the next lexicographically larger permutation after [1, 2, 3].
✗ Incorrect
The next permutation after [1, 2, 3] is [1, 3, 2] because we swap the last increasing pair and reverse the suffix.
❓ Predict Output
intermediateNext Permutation Output for Descending Array
What will be printed after running this Rust code that applies next permutation on a descending array?
DSA Rust
fn next_permutation(nums: &mut [i32]) {
let n = nums.len();
if n < 2 { return; }
let mut i = n - 2;
while i != usize::MAX && nums[i] >= nums[i + 1] {
if i == 0 { break; }
i -= 1;
}
if nums[i] < nums[i + 1] {
let mut j = n - 1;
while nums[j] <= nums[i] {
j -= 1;
}
nums.swap(i, j);
}
nums[i + 1..].reverse();
}
fn main() {
let mut arr = vec![3, 2, 1];
next_permutation(&mut arr);
println!("{:?}", arr);
}Attempts:
2 left
💡 Hint
If the array is in descending order, the next permutation is the smallest permutation.
✗ Incorrect
The array is the highest permutation. The next permutation resets it to the lowest, which is sorted ascending.
🔧 Debug
advancedIdentify the Bug in Next Permutation Implementation
Which option contains a bug that causes incorrect next permutation for the input [1, 3, 2]?
DSA Rust
fn next_permutation(nums: &mut [i32]) {
let n = nums.len();
if n < 2 { return; }
let mut i = n - 2;
while i != usize::MAX && nums[i] > nums[i + 1] {
if i == 0 { break; }
i -= 1;
}
if nums[i] < nums[i + 1] {
let mut j = n - 1;
while nums[j] < nums[i] {
j -= 1;
}
nums.swap(i, j);
}
nums[i + 1..].reverse();
}Attempts:
2 left
💡 Hint
Check the condition that finds the first decreasing element from the right.
✗ Incorrect
Using > instead of >= skips equal elements and can cause wrong i selection, leading to incorrect permutation.
🧠 Conceptual
advancedUnderstanding the Role of Reversing the Suffix
Why does the next permutation algorithm reverse the subarray after index i instead of sorting it?
Attempts:
2 left
💡 Hint
Think about the order of the suffix before reversal.
✗ Incorrect
The suffix is in descending order, so reversing it produces the smallest ascending order suffix, which is the next permutation's requirement.
🚀 Application
expertNext Permutation on a Large Array with Duplicates
Given the array [1, 5, 1, 1, 3, 3], what is the next permutation after applying the algorithm?
Attempts:
2 left
💡 Hint
Find the first decreasing element from the right, swap with next larger, then reverse suffix.
✗ Incorrect
The first decreasing element from right is at index 3 (value 1). The next larger element to swap is at index 5 (value 3). After swapping and reversing suffix, the array becomes [1, 5, 1, 3, 3, 1].
