Lyft interview question

Dfs related coding problem from leetcode