Amazon interview question

Explain your code from the online assessment (for both problems).