Amazon Interview Question

Same as Leetcode coding problem.