Amazon Interview Question

The two questions were both difficult.