Amazon interview question

Leetcode medium question (no coding required)