670. Maximum Swap
mediumAsked at MetaGiven a non-negative integer, you may swap two digits at most once to get the maximum valued number. Meta asks this to test whether you reach for the 'find largest later digit to swap with earliest smaller digit' greedy.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Meta loops.
- Glassdoor (2026-Q1)— Meta E3/E4 onsite reports cite this as a recurring greedy problem.
- Blind (2025-11)— Recurring Meta interview question.
Problem
You are given an integer num. You can swap two digits at most once to get the maximum valued number. Return the maximum valued number you can get.
Constraints
0 <= num <= 10^8
Examples
Example 1
num = 27367236Explanation: Swap the number 2 and the number 7.
Example 2
num = 99739973Explanation: No swap.
Approaches
1. Try every swap
Enumerate every (i, j) swap, track the largest result.
- Time
- O(n^2)
- Space
- O(n)
function maximumSwapBrute(num) {
const digits = num.toString().split('');
let best = num;
for (let i = 0; i < digits.length; i++) {
for (let j = i + 1; j < digits.length; j++) {
[digits[i], digits[j]] = [digits[j], digits[i]];
const v = Number(digits.join(''));
if (v > best) best = v;
[digits[i], digits[j]] = [digits[j], digits[i]];
}
}
return best;
}Tradeoff: Quadratic. Acceptable at the constraint (n ≤ 9 digits), but the greedy O(n) is more impressive.
2. Last-occurrence greedy (optimal)
For each digit position, find the largest digit that appears AFTER it. If that digit > current and > original digit, swap.
- Time
- O(n)
- Space
- O(1)
function maximumSwap(num) {
const digits = num.toString().split('');
const lastIdx = new Array(10).fill(-1);
for (let i = 0; i < digits.length; i++) lastIdx[Number(digits[i])] = i;
for (let i = 0; i < digits.length; i++) {
for (let d = 9; d > Number(digits[i]); d--) {
if (lastIdx[d] > i) {
[digits[i], digits[lastIdx[d]]] = [digits[lastIdx[d]], digits[i]];
return Number(digits.join(''));
}
}
}
return num;
}Tradeoff: Linear. The 'lastIdx[d] > i' check finds the rightmost occurrence of a digit larger than current — swapping with the rightmost (not the earliest) gives the maximum because it places the high digit furthest to the left while keeping later high digits intact.
Meta-specific tips
Meta interviewers grade this on the greedy insight: swap the LEFTMOST smaller digit with the RIGHTMOST larger digit available. State out loud: 'I want to put a high digit as far left as possible. So I scan left-to-right; at the first position where a larger digit exists later, I swap with the RIGHTMOST occurrence of the largest such digit (to keep other high digits in place).'
Common mistakes
- Swapping with the FIRST larger digit instead of the LAST — 1993 swapped greedily gives 9193, not the correct 9931.
- Forgetting that we can swap at most ONCE.
- Iterating d from current digit DOWN to 0 instead of UP from 9 — wastes work.
Follow-up questions
An interviewer at Meta may pivot to one of these next:
- What if we could do k swaps?
- Minimum Swaps to Sort.
- What if the input were a string with letters and digits?
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why swap with the RIGHTMOST occurrence of the largest digit?
If 9 appears at positions 3 and 7 (both later than the current i), we prefer position 7 because the 9 at position 3 might still be useful for an earlier swap pass — except we only swap once. So we want the LAST 9 to move forward, keeping any 9 we already had at position 3 in place.
What if there's no improving swap?
Return the original number unchanged. The greedy loop falls through to the bottom return.
Practice these live with InterviewChamp.AI
Drill Maximum Swap and other Meta interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →