Uber Interview Question

the exact problem Leetcode 64