Amazon interview question

it was a leetcode hard question