Samsung Electronics interview question

Question based on graph traversal, bfs and dfs