Medium Data Structures Graphs
Compare BFS and DFS for graph traversal. When would you use each?
Answer
BFS (Breadth-First Search) explores neighbors level by level using a queue, optimal for finding shortest path in unweighted graphs and level-order traversal. DFS (Depth-First Search) explores as deep as possible before backtracking using recursion/stack, better for topological sorting, cycle detection, and path finding. BFS uses O(V) space for the queue; DFS uses O(h) stack space where h is the maximum depth.
IIT Certified
Master These Concepts with IIT Certification
175+ hours of industry projects. Get placed at Bosch, Tata Motors, L&T and 500+ companies.
Relevant for Roles
Software Engineer Backend Developer Algorithm Developer