Amazon Interview Question

Show me how to code this coding problem.