Implement DFS for connected components | Apple Interview Question