Amazon interview question

A easy and medium leetcode question