Domanda di colloquio di Zillow

A medium difficulty algorithm question. The question is very long, I spent a while figuring it out.