← Booking.com Interview Insights
My first instinct was greedy: find the largest digit and move it to the front.
Convert the integer to a string or list of digits. Scan from left to right to find the first digit that has a larger digit to its right; swap it with the largest such digit (rightmost if ties) to maximize the number. If no such digit exists, return the original number.
Pro tip: Clarify edge cases upfront, such as negative numbers or leading zeros, and mention that the algorithm runs in O(n) time with O(n) space, which is optimal for this problem.
Ask about input range, negative numbers, and whether leading zeros are allowed after swap. Confirm that the number is non-negative and we can swap any two digits at most once.
Convert the integer to 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 the current digit with the rightmost occurrence of that maximum digit and stop.
If no such pair is found (digits are in non-increasing order), return the original number unchanged.
Convert the modified 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.