BFS Shortest Path in a Grid
Step by Step
Spread out from the start one ring at a time with a queue, and the first time you reach a cell is by a shortest route.
Step through every ring at your own pace, then share a link to the step where you got lost.
What You Watch While Rings Spread
The queue, the rings and every cell's distance change on every step, and the visualiser tracks them for you.
Holding the queue in your head works for a small grid. By step nine, which cells wait in the queue and which are done blur together.
CodeStory's visualiser spreads the rings across your grid, cell by cell. Code lines stay highlighted beside the grid. Step forward, step back or let it play.
Try a grid where the start sits in a corner, then one where it sits in the middle. Compare how the rings grow and where the shortest path ends.
How BFS Finds the Shortest Path
Steps below match the picture, so you can follow them beside the grid.
- 1Put the start cell in the queue and mark it seen.
- 2Take the cell at the front of the queue.
- 3Add every unseen neighbour to the back of the queue, one step farther from the start.
- 4Never revisit a cell, so every cell is examined once.
- 5The first time you reach a cell is a shortest route to it.
BFS Grid Cost in Time and Space
Interviewers ask why grid BFS costs rows times cols, and the visualiser counts real operations.
- Time
- O(rows × cols)counted from the real steps on your grid
- Space
- O(rows × cols)extra memory for the queue and the seen cells
- Pattern
- GraphsQueues spread in layers, so first arrival means shortest distance.
Practise BFS on Grid Problems
Watching shows the rings and solving builds the habit.
Everything in the App in One Place
Practice, interviews and tools all start free. Swipe to see the rest.
DSA practice
Problems filed under the pattern that solves them, not a sheet.
352 problemsOpenCompany sets
Practise what one company asks in the week before your round.
463 companiesOpenCoding interview
Choose a role, write in a real editor and answer while you type.
scored reportOpenMock interviews, spoken
You answer out loud and CodeStory follows up on what you said.
32 topicsOpenDatabase round
Answer DBMS the way a panel asks it, with indexes, joins and normal forms.
in Core CSOpenOOP round
Inheritance, polymorphism and the follow-up you never rehearse.
in Core CSOpenSystem design
How real systems are built, from the primitives to the AI stack.
27 chaptersOpenSQL practice
Real schemas, your query, the rows it returns and a clear verdict.
39 questionsOpenAptitude
Quant, logical, verbal, DI and technical, timed like the real test.
280 questionsOpenOnline assessment
Get a clock, marks per question and hidden tests like a company's judge.
37 papersOpenAlgorithm visualiser
Watch it play on your own input, one step at a time.
26 algorithmsOpenAI that helps, not solves
Hints one at a time and AI review before a mentor sees your work.
100 free creditsOpenResume builder
Build it inside the app, next to the work you practised.
in the appOpenProgress and readiness
One score and the axis where you are weakest.
tracked dailyOpen
Sign up for 100 AI credits now and get 100 more every month. Start free in the app
BFS Grid Visualiser FAQs
Open the visualiser and watch the answers play out.
Open the visualiserWhat does the BFS grid visualiser show?
CodeStory's visualiser spreads from your start cell one ring at a time and plays every step on a grid you choose. It highlights the line of code being executed, and you can step forward, step back or let it play.
What is the time complexity of BFS on a grid?
BFS on a grid takes O(rows × cols) time and O(rows × cols) space. CodeStory's visualiser counts real operations as it plays, so you watch the numbers instead of trusting the formula.
Why does BFS find the shortest path on a grid?
BFS reaches cells in rings of equal distance, so the first time you reach a cell is by a shortest route.
Can I use my own input?
Yes. Type your own grid and the visualiser plays BFS on it, edge cases included.
Is the visualiser free?
Yes. You start free with 100 AI credits when you sign up and 100 every month, and watching an algorithm costs no credits.
Stuck on Grid Problems? Get a Free Callback
Tell CodeStory which grid problems stump you, and a mentor plans your next steps.
- 1CodeStory finds your starting point, from your first queue to interview ready.
- 2CodeStory explains online and campus batches, with timings for your mornings or evenings.
- 3You decide. No pressure, no payment on the call.