Mesh interview question

medium level ds algo problem