The Next Permutation problem asks you to rearrange an array of integers into the lexicographically next greater permutation in-place. If no such permutation exists (the array is already the largest permutation), rearrange it to the smallest permutation (ascending order). This Next Permutation coding problem is one of the most important two-pointer algorithms to master for interviews.
J.P. Morgan, Apple, Goldman Sachs, Microsoft, Meta, Amazon, Google, Bloomberg, Adobe, and dozens more ask this because it's a fundamental combinatorics algorithm used in permutation generators, combinatorial optimization, and sequence problems. The array and two pointers interview pattern is the core, and the three-step algorithm is both elegant and non-trivial.
Three-step two-pointer algorithm:
arr[i] < arr[i+1].Array: [1, 5, 3, 4, 2].
Next Permutation has three precisely ordered steps — memorize them as a unit. The common way to remember: "find the last 'valley', swap with the next larger from right, sort the tail." The "sort the tail" is always a reverse because the tail is already descending. Practice implementing this until it takes under 5 minutes. It's the foundation for permutation-based problems including Next Permutation variants, permutation ranking, and k-th permutation problems.