Microsoft interview question

Question 5 on leet code