Altitude AI interview question

a Leetcode style medium problem