Samsung Electronics Interview Question

Question based on graph traversal, bfs and dfs