Booking.com interview question

HackerRank test, Leetcode easy/medium difficulty.