Braze interview question

Grid BFS and sorting related problem