Grid Dynamics interview question

it was leetcode mid problems