← Booking.com Interview Insights
My first instinct was greedy and I think that's the right call, but I fumbled the implementation for a bit.
Convert the integer to a string or array of digits to simplify manipulation. Scan from left to right to find the first digit that has a larger digit to its right, then swap it with the rightmost occurrence of the maximum such digit. If no such digit exists, the number is already maximal, so return it unchanged.
Pro tip: Clarify edge cases upfront, such as negative numbers, single-digit numbers, or leading zeros after swapping, and state your assumptions. This shows thoroughness and prevents misinterpreting the problem.
Ask about input range, negative numbers, and whether leading zeros are allowed. Confirm that at most one swap is permitted, including zero swaps.
Transform the integer into a string or list of characters to easily access and swap digits.
Traverse from left to right; for each digit, find the maximum digit to its right. If a larger digit exists, swap with the rightmost occurrence of that maximum and stop.
If no beneficial swap is found, return the original number as it is already the largest possible.
After the swap (or no swap), convert the digit sequence back to an integer and return it.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.