Amazon interview question

To code a solution to leetcode style question