Amazon Interview Question

A leetcode style question with some follow-ups