Amazon Interview Question

I dont know how to write the second code.