Bloomberg interview question

A DFS problem from leetcode