← Booking.com Interview Insights

Booking.com·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Got a coding question from Booking.com that looked straightforward on the surface. One swap, maximize the number. Took me longer than I'd like to admit to get the logic clean.

Questions Asked (1)

Q1

Given an integer, you're allowed to swap any two of its digits at most once. Return the largest possible number you can form.

Algorithms & Data Structures
Author's notes

My first instinct was greedy and I think that's the right call, but I fumbled the implementation for a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

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.

1. Clarify constraints and edge cases

Ask about input range, negative numbers, and whether leading zeros are allowed. Confirm that at most one swap is permitted, including zero swaps.

2. Convert to mutable sequence

Transform the integer into a string or list of characters to easily access and swap digits.

3. Identify the best swap

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.

4. Handle no-swap scenario

If no beneficial swap is found, return the original number as it is already the largest possible.

5. Convert back and return

After the swap (or no swap), convert the digit sequence back to an integer and return it.

Key Points to Mention

  • Time complexity: O(n) where n is the number of digits, with at most two passes.
  • Space complexity: O(n) for the digit array/string, or O(1) if modifying in place with careful handling.
  • Greedy strategy: always swap the leftmost digit that can be increased with the largest possible digit to its right.
  • Rightmost occurrence of the maximum digit ensures the smallest sacrifice when swapping.
  • Edge cases: single-digit numbers, numbers with all identical digits, and negative numbers (if allowed).
  • Alternative approach: using a stack or tracking last occurrence of each digit for a one-pass solution.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.