Amazon interview question

It was a standard leetcode coding question.