Amazon interview question

Medium difficulty leet code questions.