Amazon interview question

Write the function immediately