Amazon interview question

What was your thought process when writing this code?