Amazon interview question

Coding on medium and hard leetcode.