Amazon interview question

Leetcode style coding problem