Amazon Interview Question

easy level leetcode question, then a medium level followed-up question