Amazon interview question

Leetcode 532142: Hard is this