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
Coding & Algorithms
Software Engineer
Atlassian
February 9, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

6

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
Time and space complexity analysis
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
Hash maps and frequency counting
Tree structures and recursion
Data structure selection and trade-offs
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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:

  1. Initialize an empty list visited to keep track of the nodes we have already ...

Submit Your Answer
Markdown supported

Related Questions