Google Interview Question

Abstract Problem boiling down to DFS