Next Lexicographic Build Configuration From a Digit Array
Last updated: November 29, 2026
Quick Overview
A build configuration is encoded as an array of digits. Rearrange it in place into the next lexicographically greater permutation; if it is already the largest, rearrange it into the smallest (sorted ascending).
0
0
0 solved
A build configuration is encoded as an array of digits. Rearrange it in place into the next lexicographically greater permutation; if it is already the largest, rearrange it into the smallest (sorted ascending).
Asked in Google onsites to test careful array manipulation, framed as enumerating test configurations in order for a CI matrix. The interviewer looks for the pivot, swap, reverse algorithm with a crisp explanation of why the suffix is non-increasing.
What the Interviewer Expects
- Finds the rightmost index i with a[i] < a[i+1]
- Finds the rightmost element in the suffix greater than a[i] and swaps
- Reverses the suffix to make it smallest
- Handles the fully descending case by reversing everything
- Explains correctness with the non-increasing suffix invariant
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
- Generate the previous permutation instead.
- Return the k-th permutation directly without iterating.
- How does duplicate digits affect correctness?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list
aof digits (duplicates allowed). - Output:
amodified in place into the next greater permutation, or the ascending order if none exists. - O(1) extra space....
High-Level Design
Scan from the right to find the first index i where a[i] < a[i+1]. Everything to the right of i is non-increasing, so that suffix is already its largest arrangement; to get the next permutation ...