Amazon interview question

Coding Q similar to LeetCode