Booking.com interview question

Swap two digits from an integer, the result should be the maximum. For example 1682 -> 8126

Interview Answers

Anonymous

22 Sept 2017

It should be 8621!

1

Anonymous

22 Sept 2017

Yep you're right. Time to apply!

Anonymous

1 May 2018

Choose two integers to SWAP, It is not place two numbers and and then move all the others so 1682 would first swap 8 and 1 => 8612 and then IF swapping is mandatory, you have to swap 6 and 1 to be able to get the maximum of all possibilities.

Anonymous

1 May 2018

You guys are right 8621 is the answer:D

Anonymous

7 Oct 2018

As a team leader, real world answer would be to split this into technical subproblems, suggest approaches and help PM with assigning them to you fellow team members ;-D Hahaha... No. 0. Guess out that max will be if you put biggest digit in front. 1. Make list L from N (N % 10, N :=N // 10). 2. Find max M in L and swap L[len(L)-1] with L[M] 3. Make N back from list (N += L[i] * (10**i))