Amazon interview question

LeetCode Medium and sometimes hard question