Amazon Interview Question

Solve the coding question they provide