Amazon interview question

Solve the coding question they provide