Maximize a Promo Code Number With at Most One Digit Swap
Last updated: November 29, 2026
Quick Overview
Given a non-negative integer, you may swap two of its digits at most once. Return the largest number you can produce.
Meta
November 29, 20260
0
0 solved
Given a non-negative integer, you may swap two of its digits at most once. Return the largest number you can produce.
A short greedy problem in the style Meta uses for technical screens. The interviewer checks that you find the leftmost position that can improve and swap it with the last occurrence of the largest larger digit to its right, achieving linear time.
What the Interviewer Expects
- Explains why the leftmost improvable position matters most
- Records the last index of each digit
- Picks the largest digit to the right, using its last occurrence
- Achieves O(d) time for d digits
- Handles numbers already maximal
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
- Allow up to k swaps; what is the approach and cost?
- Return the smallest number instead, without a leading zero.
- Find the maximum with exactly one swap (a swap is mandatory).
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 is between 0 and 10^8. Swapping is optional. Output an integer. Target O(number of digits)....
High-Level Design
Scan digits left to right. At position i, the best improvement is to bring in the largest digit greater than digits[i] that appears to the right. Among duplicates of that digit, take the last occurr...