Grid (CA) interview question

Solving technical DS problems at home. Worked on it over a few days. Fun problem