Next Permutation
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The data structure here is just a plain array, and that's the whole point. Bloomberg reported this Next Permutation OA in November 2022, and it rewards candidates who see the structure in the array instead of reaching for anything fancy. You get nums, duplicates allowed, values can be negative, and you return the next lexicographic ordering or wrap to the smallest. It looks like a permutation generator problem. It isn't. It's a short scan, a swap, and a reverse. If you blank mid-assessment, StealthCoder is the invisible safety net running on your screen while you work.
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 the suffix. Scan from the right to find the first index i where nums[i] < nums[i+1]. That's the pivot. Everything right of it is non-increasing, so it's already the largest arrangement of those values. Scan from the right again, find the first value strictly greater than nums[i], swap them, then reverse the suffix so it becomes the smallest. If no pivot exists, reverse the whole array for the wraparound. 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. Another trap is generating permutations, which is hopeless at length 10000. This runs O(n) time and O(1) extra space. If the pivot logic slips away under the clock, StealthCoder can surface the full approach live without the proctor seeing it.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
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 Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Next Permutation FAQ
What's the trick to Next Permutation?+
Find the rightmost index where nums[i] < nums[i+1], swap it with the rightmost value larger than it, then reverse everything after i. If no such index exists, reverse the whole array. That's three simple passes and no recursion.
How do duplicates change the solution?+
They only matter for comparison strictness. The pivot needs nums[i] < nums[i+1], strictly. The swap target must be strictly greater than nums[i]. Using less-or-equal makes you pick a wrong pivot on inputs like [-1,0,-1] or [2,3,1,3,3].
What complexity should I aim for?+
O(n) time and O(1) extra space, modifying the array in place. With length up to 10000, generating permutations or sorting repeatedly is the wrong direction. Each of the scans and the reverse is linear, so the total stays linear.
Is this Bloomberg question still worth preparing?+
It was reported in November 2022 and it's a classic array problem, so expect similar in-place manipulation questions. Know the pivot, swap, reverse sequence cold. You can write it in about ten lines once the idea is clear.
How do I prepare in 48 hours?+
Write it from memory three times, then test on [1,2,3], [3,2,1], [-1,0,-1], and [2,3,1,3,3]. Say the reason for reversing the suffix out loud. If you can explain why the suffix is descending, you won't forget the steps.