Amazon Interview Question

Just a leetcode medium/hard problem