Software Engineer (SDE)dsaintermediate3 interactive MCQs in app
BFS & DFS on Graphs
mark visited on BFS enqueue, not pophandling cycles and disconnected componentsBFS unweighted shortest path vs DFS flood-fill
Core Concept
Graphs can contain CYCLES and disconnected components, so both BFS and DFS require a `visited` set and an outer `for node in range(n)` loop! For **BFS**, **mark nodes `visited` AT ENQUEUE TIME** (right when pushing into `deque`), NEVER only after popping—otherwise multiple neighbors push the same node repeatedly into the queue causing exponential blowups!
Worked Example
BFS counts unweighted shortest path distances (`dist[v] = dist[u] + 1`) cleanly, while DFS excels at connected-component flood-fill (`Number of Islands`) and backtracking paths.
Common Pitfall
marking `visited` on pop instead of enqueue in BFS, or skipping outer component loops.
Recommended Readings & Practice
Practice BFS & DFS on Graphs with 3 graded MCQs & written feedback.
Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.