Next Permutation
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon reported this one in September 2026, and the statement has a detail that trips people: duplicates are allowed, and if the array is already the greatest ordering you return the smallest. That's Next Permutation, an array problem with a known O(n) trick. If you've seen it, you're fine. If you haven't, the pivot logic is easy to fumble under a clock. Arrays go up to 10000 elements with signed 32-bit values, so brute-forcing permutations is dead on arrival. StealthCoder sits invisibly on your screen as a safety net if your mind goes blank mid-assessment, but the pattern below is short enough to memorize tonight.
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 three-step scan from the right. First, find the pivot: the rightmost index i where nums[i] < nums[i+1]. Second, if a pivot exists, find the rightmost index j to the right of it where nums[j] > nums[i], and swap them. Third, reverse everything after the pivot. If no pivot exists, the array is descending, so just reverse the whole thing. That covers the wraparound case. The pitfall is duplicates. Use strict comparisons, not <= or >=. Example 3, [2,3,1,3,3], breaks if you pick the wrong swap target or use non-strict checks. Values like -1 and 0 don't matter since you only compare. It's in-place with O(1) extra space. If you blank on the reversal step during the live OA, StealthCoder is the hedge that gives you the working code while you keep typing.
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 Amazon's OA.
Amazon 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?+
Scan from the right for the first index where nums[i] < nums[i+1]. Swap it with the rightmost larger element to its right, then reverse the suffix after i. If no such index exists, reverse the whole array. It's O(n) time and O(1) space.
How do duplicates change the solution?+
Use strict comparisons. The pivot needs nums[i] < nums[i+1], not <=. When picking the swap target, take the rightmost element strictly greater than the pivot. Example 3, [2,3,1,3,3] to [2,3,3,1,3], is the case that catches sloppy equality checks.
What should I return when the array is the largest permutation?+
Return the smallest ordering, which is the array sorted ascending. You get it by reversing the whole array, since a fully descending array has no pivot. Example 2, [-1,0,-1] giving [0,-1,-1], shows the normal pivot case, but [3,2,1] is the wraparound.
How hard is this really for an Amazon OA?+
Medium on paper, easy once you know the pattern. The logic is short, but it's hard to derive from scratch under pressure. With arrays up to 10000, you can't generate permutations, so you need the linear scan. Know it cold and it's five minutes of typing.
How do I prepare in 48 hours?+
Hand-trace the three examples on paper until the pivot, swap, reverse sequence is automatic. Then code it from memory twice. Test edge cases: length 1, all equal values, fully descending, and signed negatives. Don't memorize code, memorize the three steps.