44 breadth-first search (BFS) interview questions [easy, medium, hard]

To ace your coding interview for a software engineering job, you’ll need to understand breadth-first search. It comes up frequently in coding interviews and is fundamental to many other algorithms too.

Let’s take a look at some typical breadth-first search questions.

5 typical breadth-first search interview questions

  • Given the root of a binary tree, return the average value of the nodes on each level in the form of an array.

  • You have an undirected, connected graph of n nodes labeled from 0 to n-1. You are given an array graph where graph[i] is a list of all the nodes connected with node i by an edge. Return the length of the shortest path that visits every node.

  • Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e. from left to right, level by level).

  • Given an array of integers arr, you are initially positioned at the first index of the array. Return the minimum number of steps to reach the last index of the array.

  • Given a binary tree, find its minimum depth.

Below, we take a look at 44 breadth-first questions and provide you with links to high quality solutions to them.

This is an overview of what we’ll cover:

  1. Easy breadth-first search interview questions
  2. Medium breadth-first search interview questions
  3. Hard breadth-first search interview questions
  4. How to prepare for a coding interview

Let's get started.

1. Easy breadth-first search interview questions

You might be tempted to try to read all of the possible questions and memorize the solutions, but this is not feasible. Interviewers will always try to find new questions, or ones that are not available online. Instead, you should use these questions to practice the fundamental concepts of breadth-first search.

As you consider each question, try to replicate the conditions you’ll encounter in your interview. Begin by writing your own solution without external resources in a fixed amount of time.

If you get stuck, go ahead and look at the solution, but then try the next one alone again. Don’t get stuck in a loop of reading as many solutions as possible! We’ve analysed dozens of questions and selected ones that are commonly asked and have clear and high quality answers.

Here are some of the easiest questions you might get asked in a coding interview. These questions are often asked during the “phone screen” stage, so you should be comfortable answering them without being able to write code or use a whiteboard.

Question 1: Maximum depth of binary tree
Question 2: Minimum depth of binary tree
Question 3: Maximum depth of N-ary tree
Question 4: Average of levels in a binary tree
Question 5: Cousins in binary tree
Question 6: Find if path exists in graph

2. Medium breadth-first search interview questions

Here are some moderate-level questions that are often asked in a video call or onsite interview. You should be prepared to write code or sketch out the solutions on a whiteboard if asked.

Question 7: Binary tree level order traversal
Question 8: Binary tree zigzag level order traversal
Question 9: Binary tree level order traversal II
Question 10: Clone graph
Question 11: Populating next right pointers in each node II
Question 12: Binary tree right side view
Question 13: Number of islands
Question 14: Course schedule
Question 15: Course schedule II
Question 16: Perfect squares
Question 17: Coin change
Question 18: Minimum height trees
Question 19: Water and jug problem
Question 20: Evaluate division
Question 21: N-ary tree level order traversal
Question 22: Find bottom left tree value
Question 23: Find largest value in each tree row
Question 24: Minesweeper
Question 25: Coloring a border
Question 26: Minimum jumps to reach home
Question 27: Map of highest peak

3. Hard breadth-first search interview questions

Similar to the medium section, these more difficult questions may be asked in an onsite or video call interview. You will likely be given more time if you are expected to create a full solution.

Question 28: Word ladder
Question 29: Word ladder II
Question 30: Cut off trees for golf event
Question 31: Sliding puzzle
Question 32: Bus routes
Question 33: Shortest path visiting all nodes
Question 34: K-Similar strings
Question 35: Shortest path to get all keys
Question 36: Escape a large maze
Question 37: Minimum moves to move a box to their target location
Question 38: Minimum number of flips to convert binary matrix to zero matrix
Question 39: Shortest path in a grid with obstacles elimination
Question 40: Jump game IV
Question 41: Minimum cost to make at least one valid path in a grid
Question 42: Frog position after T seconds
Question 43: Last day where you can still cross
Question 44: Minimum cost to reach destination in time

4. How to prepare for a coding interview

4.1 Refresh your knowledge

Before you start practicing interviews, you’ll want to make sure you have a strong understanding of not only breadth-first search but also the rest of the relevant algorithms and data structures. Check out our guides for detailed explanations and useful cheat sheets.

Algorithms explained:

Data structure guides:

4.2 Practice on your own

Once you’re confident in all of these, you’ll want to start working through lots of coding problems.

For breadth-first search you can obviously use the questions we’ve listed above. For the other algorithms you need to know, check out the rest of this series:

We also recommend working through our list of 73 data structure interview questions.

One of the main challenges of coding interviews is that you have to communicate what you are doing as you are doing it. Talking through your solution out loud is therefore very helpful. This may sound strange to do on your own, but it will significantly improve the way you communicate your answers during an interview.

It's also a good idea to work on a piece of paper or google doc, given that in the real interview you'll have to code on a whiteboard or virtual equivalent.

3.3 Practice with others

Of course, if you can practice with someone else playing the role of the interviewer, that's really helpful. But sooner or later you’re probably going to want some expert interventions and feedback to really improve your coding interview skills.

That’s why we recommend practicing with ex-interviewers from top tech companies. If you know a software engineer who has experience running interviews at a big tech company, then that's fantastic. But for most of us, it's tough to find the right connections to make this happen. And it might also be difficult to practice multiple hours with that person unless you know them really well.

Here's the good news. We've already made the connections for you. We’ve created a coaching service where you can practice 1-on-1 with ex-interviewers from leading tech companies. Learn more and start scheduling sessions today.

cta_illustration arrow_left Browse FAANG ex-interviewers

Any questions about breadth-first search?

If you have any questions about breadth-first search or coding interviews in general, don't hesitate to ask them in the comments below. All questions are good questions, so go ahead!