Bird
Raised Fist0
DSA Typescriptprogramming~10 mins

Next Permutation of Array in DSA Typescript - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to find the next permutation by swapping the rightmost pair where the left element is smaller.

DSA Typescript
for (let i = nums.length - 2; i >= 0; i--) {
  if (nums[i] [1] nums[i + 1]) {
    // Found the first decreasing element
    break;
  }
}
Drag options to blanks, or click blank then click option'
A>
B==
C<
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' causes the loop to find the wrong pivot.
Using '==' or '!=' does not help find the pivot.
2fill in blank
medium

Complete the code to find the element to swap with the pivot found at index i.

DSA Typescript
for (let j = nums.length - 1; j > i; j--) {
  if (nums[j] [1] nums[i]) {
    // Swap nums[i] and nums[j]
    [nums[i], nums[j]] = [nums[j], nums[i]];
    break;
  }
}
Drag options to blanks, or click blank then click option'
A>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causes swapping with a smaller element.
Using '==' or '!=' does not find the correct swap candidate.
3fill in blank
hard

Fix the error in reversing the suffix of the array after swapping.

DSA Typescript
let start = i + 1;
let end = nums.length - 1;
while (start [1] end) {
  [nums[start], nums[end]] = [nums[end], nums[start]];
  start++;
  end--;
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes swapping the middle element twice.
Using '>=' or '>' causes the loop to never run or run incorrectly.
4fill in blank
hard

Fill both blanks to complete the function that finds the next permutation of the array.

DSA Typescript
function nextPermutation(nums: number[]): void {
  let i = nums.length - 2;
  while (i >= 0 && nums[i] [1] nums[i + 1]) {
    i--;
  }
  if (i >= 0) {
    let j = nums.length - 1;
    while (nums[j] [2] nums[i]) {
      j--;
    }
    [nums[i], nums[j]] = [nums[j], nums[i]];
  }
  let start = i + 1;
  let end = nums.length - 1;
  while (start < end) {
    [nums[start], nums[end]] = [nums[end], nums[start]];
    start++;
    end--;
  }
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' or '<=' in these loops causes incorrect pivot or swap index.
Mixing up the comparison operators leads to wrong next permutation.
5fill in blank
hard

Fill all three blanks to implement the next permutation function correctly.

DSA Typescript
function nextPermutation(nums: number[]): void {
  let i = nums.length - 2;
  while (i >= 0 && nums[i] [1] nums[i + 1]) {
    i--;
  }
  if (i >= 0) {
    let j = nums.length - 1;
    while (nums[j] [2] nums[i]) {
      j--;
    }
    [nums[i], nums[j]] = [nums[j], nums[i]];
  }
  let start = i + 1;
  let end = nums.length - 1;
  while (start [3] end) {
    [nums[start], nums[end]] = [nums[end], nums[start]];
    start++;
    end--;
  }
}
Drag options to blanks, or click blank then click option'
A<=
B<
C>=
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' in the last loop causes double swapping of the middle element.
Using '<' or '<=' incorrectly in the first two loops causes wrong pivot or swap index.

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