Amazon interview question

The two questions were both difficult.