All guides

Step 3 of 5: Pass the coding test

BFS and DFS Templates: Six Templates, Java and Python

Free

No payment. Just your email.

  • 14 pages
  • 6 templates
  • Tree, graph, grid

About this guide

The six traversal templates every fresher is asked to write: BFS and DFS for a binary tree, for a graph stored as an adjacency list, and for a 2D grid, each in Python and the same lines in Java, each with its visited-set rule stated. Then a page on when BFS beats DFS and when DFS is simpler, four fully worked problems with traces (level order, number of islands, shortest path in a grid with obstacles, cycle in an undirected graph), and the five mistakes that cost marks. Every Python snippet was run and matches the output printed beside it. 14 pages, PDF.

What you download

  • BFS_And_DFS_Templates.pdf846 KB