Amazon interview question

I dont know how to write the second code.