Microsoft Interview Question

Question 5 on leet code