Tesla interview question

Some medium hard Leetcode question