Amazon interview question

Explaining the whole code orally.