Amazon Interview Question

Explaining the whole code orally.