DSA · INTERVIEW & OA PREP

Topic 4 of 12

Trees & Graphs

Trees and graphs are where medium/hard interviews live. DFS and BFS are the two hammers; most problems are a variation of traversal plus some bookkeeping. Recognising the graph hidden inside a grid or a set of dependencies is half the battle.

Key concepts

  • DFS (recursion/stack) vs BFS (queue) — BFS finds shortest paths in unweighted graphs.
  • Binary tree traversals: inorder, preorder, postorder, and level-order.
  • Detect cycles, count components, and topologically sort with DFS.
  • Grids and dependency lists are graphs in disguise.

Questions that get asked

  • Maximum depth / height of a binary tree.
  • Level-order traversal (BFS).
  • Number of islands (grid DFS/BFS).
  • Lowest common ancestor of two nodes.
  • Course schedule / detect a cycle in a directed graph.
Tip

Ask whether the tree is a BST — it unlocks O(log n) search and ordered traversal shortcuts.

Common mistake

Forgetting a visited set on graphs causes infinite loops. Trees don't need one; general graphs do.

Quick checks

Three tap-only questions to test your recall on this topic.

Keep going

Ready to apply? Browse live internships and sharpen your resume first.