Next Permutation
Reported by candidates from Oracle's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Brute force dies here. With nums.length up to 10000, generating every permutation and sorting them is hopeless, since the count grows factorially. Oracle reported this Next Permutation OA in September 2026, and it's a classic array problem with a linear trick. The twist in this version is that you return the new array, and duplicates plus negative values are allowed. If you know the pivot-swap-reverse routine, it's five minutes of work. If you blank, StealthCoder is the invisible safety net running during the live OA, so one memory lapse doesn't sink the attempt.
The problem
Return the lexicographically next permutation of nums. If the current ordering is the greatest possible, return the smallest ordering. Duplicate values are allowed. Function nextPermutation(nums: int[]) → int[] Examples Example 1 nums = [1,2,3] return = [1,3,2] Swapping the final two values gives the next greater ordering. Example 2 nums = [-1,0,-1] return = [0,-1,-1] Covers wraparound, duplicates, pivot placement, tiny arrays, equality, and signed values. Example 3 nums = [2,3,1,3,3] return = [2,3,3,1,3] Covers wraparound, duplicates, pivot placement, tiny arrays, equality, and signed values. Constraints 1 <= nums.length <= 10000 Values fit in a signed 32-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is a single right-to-left scan. Find the pivot: the rightmost index i where nums[i] < nums[i+1]. If none exists, the array is the largest ordering, so reverse the whole thing to get the smallest. Otherwise scan from the right again for the first element strictly greater than nums[pivot], swap them, then reverse the suffix after the pivot. The suffix was descending, so reversing makes it the smallest possible tail. The common pitfall is duplicates. Use strict less-than for the pivot and strict greater-than for the swap target, or [2,3,1,3,3] breaks. Check [-1,0,-1] by hand: pivot is index 0, swap with the last -1... wait, the first element greater than -1 from the right is 0, giving [0,-1,-1] after reversing. That's O(n) time and O(1) extra space apart from the returned copy. If the live OA has you freezing on the swap step, StealthCoder can surface the routine.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Next Permutation cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as next permutation. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Oracle's OA.
Oracle reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Next Permutation FAQ
What's the trick for Next Permutation?+
Find the rightmost pivot where nums[i] < nums[i+1], swap it with the rightmost element larger than it, then reverse everything after the pivot. If no pivot exists, reverse the whole array. It's one pass plus a reverse, so O(n) total.
How do duplicates change the solution?+
Use strict comparisons. The pivot needs nums[i] < nums[i+1], not less-or-equal, and the swap target must be strictly greater than the pivot. Non-strict checks pick the wrong index on inputs like [2,3,1,3,3] and give an incorrect result.
Why can't I just generate all permutations?+
With up to 10000 elements, the permutation count is astronomically large. Even sorting a handful of them would time out. The input size is the hint that you need a linear scan that builds the answer directly from the current ordering.
What edge cases should I test before submitting?+
Test a single element, a fully descending array like [3,2,1] that wraps to ascending, all-equal values, negatives like [-1,0,-1], and a pivot at index 0. Also confirm you return the array in the form the function signature expects.
How do I prepare for this in 48 hours?+
Code the pivot, swap, reverse routine from scratch three times without looking. Then trace the three examples by hand. This problem is pure pattern recall, so repetition beats reading. Write the helper that reverses a range so you don't fumble indices under pressure.