Amazon interview question

Leetcode Hard Question was asked