Amazon Interview Question

An algorithm question on leetcode