Bfs

39 articles

dsa6 min read

BFS and DFS on Graphs and Grids — The Complete Interview Guide

Master BFS and DFS on graphs and grids with the seven core patterns that show up in 90 percent of FAANG graph interviews. Learn flood fill, multi-source BFS, shortest path on unweighted graphs, and connected components with Python and JavaScript code.

Read →
dsa10 min read

Walls and Gates — Multi-Source BFS Every FAANG Grid Interview Tests

LC 286 Walls and Gates asks you to fill each empty room with its distance to the nearest gate. The key insight is to flip the direction: instead of BFS from every room, do multi-source BFS from all gates simultaneously and push distances outward in O(m*n) time.

Read →
dsa11 min read

Minimum Effort Path — Dijkstra or Binary Search + BFS

Find the path from top-left to bottom-right that minimizes the maximum absolute difference between consecutive cells. Dijkstra treats effort as edge weight; binary search + BFS checks feasibility for each candidate effort.

Read →
dsa9 min read

Snakes and Ladders — BFS on Board State

Minimum dice rolls to reach square n² from square 1. The hard part is converting square numbers to board coordinates in Boustrophedon (snake) order. BFS on the state space of squares gives the optimal answer.

Read →
dsa8 min read

Open the Lock — BFS on State Space

Find the minimum number of turns to go from "0000" to the target combination on a 4-wheel lock, avoiding deadend states. Classic BFS on a finite state space — the lock combination is the node, each wheel turn is an edge.

Read →
dsa9 min read

Cheapest Flights Within K Stops — Bellman-Ford with a Twist

LC 787 Cheapest Flights Within K Stops asks for the cheapest route from src to dst using at most k intermediate stops. Bellman-Ford with exactly k+1 relaxation rounds handles the stop constraint cleanly without Dijkstra getting confused by the layered state.

Read →
dsa10 min read

Word Ladder — BFS on an Implicit Graph of Word Transformations

LC 127 Word Ladder asks for the shortest sequence of one-letter transformations from beginWord to endWord, where every intermediate word must be in the dictionary. BFS on the implicit word-state graph finds the minimum in O(M^2 * N) using wildcard pattern grouping.

Read →
dsa7 min read

Mock Week 3 — Hard Problems Under Time Pressure

Week 3 of the FAANG mock program pairs Burst Balloons and Word Ladder II as hard problems under timed pressure. Learn the stuck-recovery protocol, the inversion insight, and why partial credit plus continuous communication beats silence every time.

Read →
dsa6 min read

Populate Next Right Pointers in Each Node — LC 116 O(1) Space BFS

LC 116 Populate Next Right Pointers asks you to connect each node to its next right sibling. The O(1) space solution leverages already-connected next pointers on the current level to wire up the next level — a pattern tested at Amazon and Microsoft.

Read →
dsa6 min read

All Nodes Distance K in Binary Tree — LC 863 BFS with Parent Map

LC 863 All Nodes Distance K in Binary Tree finds all nodes exactly K edges from a target. The key insight is converting the tree to an undirected graph by recording parent pointers, then running BFS from the target — a pattern tested at Amazon and Google.

Read →
dsa6 min read

Serialize and Deserialize Binary Tree — LC 297 FAANG Hard

LC 297 Serialize and Deserialize Binary Tree is a top FAANG hard problem asked at Facebook, Amazon, and Google. The DFS preorder approach with null markers gives a compact O(n) codec; BFS level-order is more intuitive. Both are valid interview answers.

Read →