Skip to content

Breadth First Search
Problems and Patterns

CodeStory tags 157 problems as Breadth First Search, and 100 of them are Medium. Learn the queue trick, then solve the ten most asked.

Sign up free and receive 100 AI credits, then 100 more monthly.

How the Breadth First Search Pattern Works

CodeStory covers the tell, the queue mechanism and the weight mistake.

Spot the tell. You need the shortest path in steps, or your problem spreads out one layer at a time. Study CodeStory's Breadth First Search guide before you code.

Learn the mechanism. Use a queue and a seen set. Everything one step away comes out before anything two steps away, so you reach a node first by the shortest route. Practise the queue in CodeStory's queue problems and grid walks in its matrix problems.

Avoid the mistake. BFS finds the shortest path only when every edge has the same weight. Edges with different weights call for Dijkstra, which you can step through in CodeStory's Dijkstra visualiser.

Most Asked Breadth First Search Problems

CodeStory lists these by how many companies set them.

  1. Walls and GatesMedium7 companies
  2. Shortest Path in a Grid with Obstacles EliminationHard6 companies
  3. Number of Connected Components in an Undirected GraphMedium3 companies
  4. Find the Safest Path in a GridMedium2 companies
  5. As Far from Land as PossibleMedium2 companies
  6. Island PerimeterEasy1 company
  7. Detonate the Maximum BombsMedium1 company
  8. Find All Groups of FarmlandMedium1 company
  9. Minimum Cost to Make at Least One Valid Path in a GridHard1 company
  10. Minimum Number of Operations to Make X and Y EqualMedium1 company

Study, Watch and Solve Breadth First Search

Follow CodeStory's order to make layers stick.

BFS Problems FAQs

Start free with no card, or ask a mentor on a free call.

Start Practising BFS
How do I know a problem is a BFS problem?

CodeStory looks for two signs. You need the shortest path in steps, or your problem spreads out one layer at a time.

How many BFS problems should I solve?

CodeStory recommends ten to fifteen across the levels, which is enough to stop needing the hint. CodeStory shows all 157 tagged problems, split 15 Easy, 100 Medium and 42 Hard, so expect mostly Medium.

Which mistake costs the most in BFS problems?

Using BFS for shortest path when edges have different weights costs the most. Those problems need Dijkstra.

Why does BFS find the shortest path?

BFS visits everything one step away before anything two steps away, so the first time you reach a node is by the shortest route.

Is practising by pattern better than a problem sheet?

Yes. Sheets give you problems in a row, while CodeStory's patterns show you what links them, and that link carries over to problems you have not seen. CodeStory files every problem under its pattern.

Which BFS problems do companies ask most?

Walls and Gates leads, asked at 7 companies in CodeStory's catalogue. Next comes a grid problem with obstacles, asked at 6 companies.

Is BFS practice free to start?

Yes. CodeStory needs no card, gives you 100 AI credits at signup and adds 100 every month.

Want Guided BFS Practice? Get a Free Callback

Describe your target role to CodeStory, and a mentor lines up your path.

  1. 1CodeStory rates your level, from beginner to interview ready.
  2. 2CodeStory explains online and campus batches, with timings that fit your schedule.
  3. 3You decide. No pressure, no payment on the call.

Get a Free Callback

I am a
I want to learn

CodeStory dials your number only for this callback.