Bird
Raised Fist0
DSA Typescriptprogramming~20 mins

Next Permutation of Array in DSA Typescript - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Next Permutation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after applying next permutation?
Given the array [1, 3, 2], what will be the array after applying the next permutation algorithm once?
DSA Typescript
function nextPermutation(nums: number[]): void {
  let i = nums.length - 2;
  while (i >= 0 && nums[i] >= nums[i + 1]) {
    i--;
  }
  if (i >= 0) {
    let j = nums.length - 1;
    while (nums[j] <= nums[i]) {
      j--;
    }
    [nums[i], nums[j]] = [nums[j], nums[i]];
  }
  let left = i + 1, right = nums.length - 1;
  while (left < right) {
    [nums[left], nums[right]] = [nums[right], nums[left]];
    left++;
    right--;
  }
}

const arr = [1, 3, 2];
nextPermutation(arr);
console.log(arr);
A[1, 3, 2]
B[1, 2, 3]
C[3, 1, 2]
D[2, 1, 3]
Attempts:
2 left
💡 Hint
Look for the first decreasing element from the right, then swap and reverse the suffix.
❓ Predict Output
intermediate
2:00remaining
What is the output after next permutation on descending array?
Given the array [3, 2, 1], what will be the array after applying the next permutation algorithm once?
DSA Typescript
function nextPermutation(nums: number[]): void {
  let i = nums.length - 2;
  while (i >= 0 && nums[i] >= nums[i + 1]) {
    i--;
  }
  if (i >= 0) {
    let j = nums.length - 1;
    while (nums[j] <= nums[i]) {
      j--;
    }
    [nums[i], nums[j]] = [nums[j], nums[i]];
  }
  let left = i + 1, right = nums.length - 1;
  while (left < right) {
    [nums[left], nums[right]] = [nums[right], nums[left]];
    left++;
    right--;
  }
}

const arr = [3, 2, 1];
nextPermutation(arr);
console.log(arr);
A[1, 2, 3]
B[3, 1, 2]
C[3, 2, 1]
D[2, 3, 1]
Attempts:
2 left
💡 Hint
If no next permutation exists, the array resets to the lowest order.
🔧 Debug
advanced
2:00remaining
Identify the error in this next permutation implementation
What error will this code produce when run with input [1, 2, 2]?
DSA Typescript
function nextPermutation(nums: number[]): void {
  let i = nums.length - 2;
  while (i >= 0 && nums[i] > nums[i + 1]) {
    i--;
  }
  if (i >= 0) {
    let j = nums.length - 1;
    while (nums[j] < nums[i]) {
      j--;
    }
    [nums[i], nums[j]] = [nums[j], nums[i]];
  }
  let left = i + 1, right = nums.length - 1;
  while (left < right) {
    [nums[left], nums[right]] = [nums[right], nums[left]];
    left++;
    right--;
  }
}

const arr = [1, 2, 2];
nextPermutation(arr);
console.log(arr);
A[2, 1, 2]
B[1, 2, 2]
C[2, 2, 1]
DRuntime error: Index out of bounds
Attempts:
2 left
💡 Hint
Check the comparison operators in the while loops carefully.
🧠 Conceptual
advanced
1:00remaining
How many permutations are possible for array [1, 2, 3]?
Considering all unique permutations of the array [1, 2, 3], how many distinct permutations exist?
A9
B3
C6
D12
Attempts:
2 left
💡 Hint
Number of permutations of n distinct elements is n factorial (n!).
❓ Predict Output
expert
3:00remaining
What is the 4th next permutation of [1, 2, 3]?
Starting from the array [1, 2, 3], if you apply the next permutation algorithm 4 times consecutively, what will be the resulting array?
DSA Typescript
function nextPermutation(nums: number[]): void {
  let i = nums.length - 2;
  while (i >= 0 && nums[i] >= nums[i + 1]) {
    i--;
  }
  if (i >= 0) {
    let j = nums.length - 1;
    while (nums[j] <= nums[i]) {
      j--;
    }
    [nums[i], nums[j]] = [nums[j], nums[i]];
  }
  let left = i + 1, right = nums.length - 1;
  while (left < right) {
    [nums[left], nums[right]] = [nums[right], nums[left]];
    left++;
    right--;
  }
}

const arr = [1, 2, 3];
for(let k = 0; k < 4; k++) {
  nextPermutation(arr);
}
console.log(arr);
A[3, 1, 2]
B[2, 3, 1]
C[3, 2, 1]
D[1, 3, 2]
Attempts:
2 left
💡 Hint
List permutations in order and count four steps from the start.

Practice

(1/5)
1. What does the next permutation of an array represent?
easy
A. The smallest element moved to the front
B. The array sorted in ascending order
C. The array reversed
D. The next lexicographically greater arrangement of the array elements

Solution

  1. Step 1: Understand permutation order

    Permutations can be ordered lexicographically like words in a dictionary.
  2. Step 2: Define next permutation

    The next permutation is the immediate next arrangement that is greater than the current one.
  3. Final Answer:

    The next lexicographically greater arrangement of the array elements -> Option D
  4. Quick Check:

    Next permutation means next bigger order [OK]
Hint: Next permutation means next bigger order of elements [OK]
Common Mistakes:
  • Thinking next permutation is just sorting ascending
  • Confusing next permutation with reversing the array
  • Assuming next permutation always moves smallest element front
2. Which of the following TypeScript function signatures correctly represents a function to compute the next permutation of an array of numbers?
easy
A. function nextPermutation(nums: number[]): void
B. function nextPermutation(nums: string): string
C. function nextPermutation(nums: number): number[]
D. function nextPermutation(nums: any): any[]

Solution

  1. Step 1: Check parameter type

    The input should be an array of numbers, so nums: number[] is correct.
  2. Step 2: Check return type

    The function modifies the array in place and returns nothing, so void is correct.
  3. Final Answer:

    function nextPermutation(nums: number[]): void -> Option A
  4. Quick Check:

    Input array, no return needed [OK]
Hint: Input array of numbers, modify in place, return void [OK]
Common Mistakes:
  • Using string type instead of number array
  • Returning a new array instead of modifying in place
  • Using incorrect parameter types
3. What is the output array after applying the next permutation algorithm on [1, 3, 2]?
medium
A. [2, 1, 3]
B. [1, 2, 3]
C. [3, 1, 2]
D. [1, 3, 2]

Solution

  1. Step 1: Find the pivot index i

    Start from i = nums.length - 2 = 1. nums[1] = 3 >= nums[2] = 2, so i-- to 0. nums[0] = 1 < nums[1] = 3, pivot i = 0.
  2. Step 2: Find successor j

    From j = nums.length - 1 = 2, find rightmost j > i with nums[j] > nums[i]. j=2: 2 > 1, yes.
  3. Step 3: Swap pivot and successor

    Swap nums[0] and nums[2]: [2, 3, 1].
  4. Step 4: Reverse suffix after i

    Reverse indices 1 to 2: [3, 1] becomes [1, 3]. Final: [2, 1, 3].
  5. Final Answer:

    [2, 1, 3] -> Option A
  6. Quick Check:

    Next permutation of [1,3,2] is [2,1,3] [OK]
Hint: Find pivot, swap with next bigger, reverse suffix [OK]
Common Mistakes:
  • Swapping wrong elements
  • Not reversing suffix after swap
  • Confusing next permutation with sorting
4. Given this TypeScript code snippet, what is the error?
function nextPermutation(nums: number[]): void {
  let i = nums.length - 2;
  while (i >= 0 && nums[i] >= nums[i + 1]) {
    i--;
  }
  if (i >= 0) {
    let j = nums.length - 1;
    while (nums[j] <= nums[i]) {
      j--;
    }
    [nums[i], nums[j]] = [nums[j], nums[i]];
  }
  nums.reverse();
}
medium
A. The while loops have wrong conditions
B. The entire array is reversed instead of just the suffix
C. The swap syntax is incorrect
D. The function returns a value but should be void

Solution

  1. Step 1: Analyze reversal step

    The code reverses the entire array with nums.reverse() instead of just the suffix after index i+1.
  2. Step 2: Understand correct reversal

    Only the part of the array after the pivot index should be reversed to get the next permutation.
  3. Final Answer:

    The entire array is reversed instead of just the suffix -> Option B
  4. Quick Check:

    Reverse suffix, not whole array [OK]
Hint: Reverse only suffix after pivot, not whole array [OK]
Common Mistakes:
  • Reversing entire array instead of suffix
  • Incorrect swap syntax
  • Wrong loop conditions causing errors
5. You have an array [5, 4, 3, 2, 1]. After applying the next permutation algorithm, what will be the resulting array?
hard
A. [5, 4, 3, 1, 2]
B. [5, 4, 3, 2, 1]
C. [1, 2, 3, 4, 5]
D. [1, 5, 4, 3, 2]

Solution

  1. Step 1: Check if array is highest permutation

    The array is in descending order, which is the highest permutation.
  2. Step 2: Apply next permutation rule for highest permutation

    If no next bigger permutation exists, the array is reversed to the lowest permutation.
  3. Final Answer:

    [1, 2, 3, 4, 5] -> Option C
  4. Quick Check:

    Descending array next permutation is ascending [OK]
Hint: Descending array next permutation is ascending order [OK]
Common Mistakes:
  • Returning same array instead of reversed
  • Trying to swap elements incorrectly
  • Confusing next permutation with partial sorting
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