Amazon Interview Question

Tricky but manageable with leetcode