Maximum Swap
MediumGreedygreedy
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Given a non-negative integer num, you may swap two of its digits at most once.
Return the largest value you can obtain. If no swap makes it larger, return num unchanged. Leading zeros never arise in a larger result, so you do not need to handle them specially.
Examples
Input: num = 1993
Output: 9913
Swap the leading 1 with the last 9.
Input: num = 8710
Output: 8710
The digits are already non-increasing, so no swap helps.
Constraints
- 0 <= num <= 10^8
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.