Amazon interview question

Just a standard LeetCode question.