Uber interview question

A top difficulty leetcode question