Amazon Interview Question

First was an exam of problem solving