Amazon interview question

Coding question was a leetcode hard question