Next and Previous Lexicographic Permutation
Updated
Problem statement
You are given an integer array nums and a string direction. Change nums in place into its neighboring permutation in lexicographic order, then return the changed array.
A permutation is any reordering of the values. To compare two permutations lexicographically, find the first position where they differ. The one with the smaller value there is smaller.
- If
directionis"NEXT", move to the permutation just after it, the next larger one. If there is no larger one, wrap around to the smallest permutation. - If
directionis"PREVIOUS", move to the permutation just before it, the next smaller one. If there is no smaller one, wrap around to the largest permutation. - Duplicate values are distinct occurrences. The swap candidate must be strictly greater than the pivot for
"NEXT"and strictly smaller for"PREVIOUS".
The direction is always one of the two uppercase strings above.
Function
adjacentPermutation(nums: int[], direction: String) → int[]Examples
Example 1
nums = [1,2,2]direction = "NEXT"return = [2,1,2]The last two values cannot be reordered into anything larger, so the first value must grow. It becomes the next larger value, and the remaining values go in increasing order. So [1,2,2] becomes [2,1,2].
Example 2
nums = [1,2,3]direction = "PREVIOUS"return = [3,2,1]The values are already in increasing order, which is the smallest permutation. Nothing comes before it, so the result wraps around to the largest permutation: the same values in decreasing order.
Constraints
1 <= nums.length <= 100000.- Every value in
numsfits a signed 32-bit integer. directionis exactly"NEXT"or"PREVIOUS".- Transform the input array in place using
O(1)auxiliary space.
More Adobe interview questions
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Lowest Common Ancestor Only When Both Targets ExistONSITE INTERVIEW · Seen Aug 2026
- Lowest Common Ancestor with Parent PointersONSITE INTERVIEW · Seen Aug 2026
- Asteroid CollisionONSITE INTERVIEW · Seen Aug 2026
- Farthest Affordable Shop per QueryONSITE INTERVIEW · Seen Aug 2026
- Minimum Removals for a Nice ArrayONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Trie: Add and Search WordsONSITE INTERVIEW · Seen Jul 2026
- Base Unit ConversionsOA · Seen Nov 2026
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Merge Sorted Squares of Two ArraysWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Sort Ones, Twos, and Threes In PlaceAgoda · PHONE SCREEN · ONSITE INTERVIEWAgoda interview questions
- Minimum Station Tracks for a Train ScheduleBNSF Railway · ONSITE INTERVIEWBNSF Railway interview questions
- 4SumAmazon · ONSITE INTERVIEW
- Count Triplets With Sum at Most TargetGoogle · ONSITE INTERVIEWGoogle interview questions
- Sort ColorsCapital One · ONSITE INTERVIEWCapital One interview questions
- 3SumOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
- 3SumMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Sort ColorsInfosys · ONSITE INTERVIEWInfosys interview questions
Adobe interview experiences
- Adobe New Grad OA: Unit Conversion and Code Review: 1 Experience
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe New Grad MTS Interview Questions: 7 Experiences
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)