Advance a Feed Ranking Order to the Next Lexicographic Arrangement
Last updated: November 29, 2026
Quick Overview
Given an array of integers representing an ordering of feed items, rearrange it in place into the next lexicographically greater permutation. If none exists, rearrange it into ascending order.
Meta
November 29, 20260
0
0 solved
Given an array of integers representing an ordering of feed items, rearrange it in place into the next lexicographically greater permutation. If none exists, rearrange it into ascending order.
Next permutation is a Meta onsite favorite because it rewards reasoning over memorization: the interviewer wants you to derive why the suffix is descending and why reversing it gives the smallest continuation. They check in-place O(n) code with duplicate values handled.
What the Interviewer Expects
- Finds the rightmost index i with a[i] < a[i+1]
- Swaps a[i] with the rightmost element greater than it in the suffix
- Reverses the suffix rather than sorting it
- Wraps to ascending order when no pivot exists
- Handles duplicates with strict comparisons in the right places
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Implement the previous permutation.
- Return the k-th permutation of 1..n directly.
- How many times must you call next permutation to cycle through a multiset?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Mutate in place with O(1) extra space. Values may repeat. For the last permutation (fully descending), produce the first (ascending)....
High-Level Design
Scan from the right for the first index i where a[i] < a[i+1]. Everything after i is non-increasing, so it is already the largest arrangement of that suffix; we must bump position i. Swap a[i] w...