Amazon interview question

Leetcode 863 was asked in interview round