Maximum Swap
A medium-tier problem at 52% community acceptance, tagged with Math, Greedy. Reported in interviews at KLA and 2 others.
You're given an integer and can swap two digits at most once to maximize it. Sounds simple until you hit the live OA and realize the greedy instinct breaks down. The trick isn't just picking the largest digit and swapping it forward. KLA, Meta, and TikTok ask this. Acceptance rate sits around 52%, which means half the room gets it wrong, usually because they swap too early or don't understand why the position of the larger digit matters. If you blank on the approach during the assessment, StealthCoder surfaces the working solution invisibly while you think through your next move.
Companies that ask "Maximum Swap"
Maximum Swap is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know.
Get StealthCoderThe greedy pattern here is counterintuitive: scan from left to right for the first digit that's smaller than something to its right, then find the largest digit to its right that's bigger than it, and swap. The catch is when you have multiple occurrences of that largest digit. You need to pick the rightmost one, not the first. Most candidates miss this and swap at the wrong position, leaving points on the table. The Math and Greedy topics here are tightly coupled. You're doing math on digit values and positions, not sorting or searching. If you're halfway through the OA and hit this problem cold, StealthCoder runs invisibly during the assessment and gives you the correct swap logic in seconds, so you can move on without the mental tax of reverse-engineering a tricky greedy rule.
Pattern tags
You know the problem.
Make sure you actually pass it.
Maximum Swap recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximum Swap interview FAQ
Why isn't it just 'find the biggest digit and swap it to the front'?+
Because you can only swap once, and position matters. Swapping the largest digit to the front doesn't always maximize the overall number. You need to find the leftmost position where a smaller digit has a larger digit to its right, then swap with the rightmost occurrence of that larger digit. Otherwise, you waste your one swap.
Is this problem still asked at Meta and TikTok?+
Yes. It appears in the reports for KLA, Meta, and TikTok. With a 52% acceptance rate, it's clearly not trivial. If you're prepping for those companies and skip this, you're gambling that it won't come up on your specific OA.
What's the actual time and space complexity I need to hit?+
You should target O(n) time where n is the number of digits, and O(1) space (or O(n) if you convert to a list to swap). The greedy one-pass scan is the intended approach. Don't overthink it with dynamic programming or recursion.
How does this relate to the Greedy topic?+
Greedy here means making a locally optimal choice: find the first position that loses value and fix it by swapping with the best candidate to its right. The key insight is that earlier positions have higher weight, so you prioritize fixing leftmost issues before considering rightmost ones.
What's the most common mistake during a live interview?+
Swapping the largest digit without checking if it's to the right of a smaller digit, or swapping the first occurrence of the target digit instead of the rightmost. Candidates also forget to handle edge cases like single digits or numbers where no swap improves the value. Test your logic on small examples first.
Want the actual problem statement? View "Maximum Swap" on LeetCode →