Amazon interview question

standard coding question from leetcode