BFS on graph
Last updated: February 9, 2026
Quick Overview
Given an undirected graph represented as an adjacency list, implement a Breadth-First Search (BFS) algorithm to traverse the graph starting from a specified source node. Your function should return a list of nodes in the order they are visited. The input will consist of the graph and the starting node, and the output should be the traversal order as an array of node values.
Atlassian
February 9, 20266
0
2,148 solved
Given an undirected graph represented as an adjacency list, implement a Breadth-First Search (BFS) algorithm to traverse the graph starting from a specified source node. Your function should return a list of nodes in the order they are visited. The input will consist of the graph and the starting node, and the output should be the traversal order as an array of node values.
This coding problem is frequently asked during Onsite at Atlassian. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Atlassian expects candidates to write production-quality code, not just solve the puzzle.
What the Interviewer Expects
- Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
- Discuss multiple approaches and trade-offs before coding
- Implement an optimal solution with clean, production-quality code
- Handle all edge cases including boundary conditions and invalid input
- Optimize both time and space complexity with clear justification
- Test your solution systematically with well-chosen examples
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you modify your solution to handle streaming input?
- What is the worst-case input for your solution?
- How would your solution change if the input was sorted?
- How would you parallelize this solution?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Problem Analysis
This problem requires us to use the Breadth-First Search (BFS) algorithm to traverse an undirected graph represented as an adjacency list. The BFS pattern is applicable here because it explores all ne...
Approach
We will implement the BFS algorithm using a queue to keep track of the nodes to be visited. The steps are as follows:
- Initialize an empty list
visitedto keep track of the nodes we have already ...