Amazon interview question

easy-medium leetcode question with help oft he interviewer