Urban Ladder interview question

Time complexity problems