Transform matrix to adjacency list

Last updated: March 10, 2026

Quick Overview

Given a matrix representation of a graph, convert it into an adjacency list format. The input will be a 2D array where each element indicates the presence of an edge between nodes, and the output should be a list of lists where each sublist contains the neighbors of the corresponding node. Ensure that the adjacency list accurately reflects the connections defined by the matrix.

Zscaler
Coding & Algorithms
Software Engineer
Zscaler
March 10, 2026
Software Engineer
Onsite
Coding & Algorithms
Easy

9

0

599 solved


Given a matrix representation of a graph, convert it into an adjacency list format. The input will be a 2D array where each element indicates the presence of an edge between nodes, and the output should be a list of lists where each sublist contains the neighbors of the corresponding node. Ensure that the adjacency list accurately reflects the connections defined by the matrix.

This coding problem is frequently asked during Onsite at Zscaler. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Zscaler expects candidates to write production-quality code, not just solve the puzzle.

What the Interviewer Expects
  • Identify the correct data structure and algorithm for the problem
  • Write clean, bug-free code with proper variable naming
  • Analyze time and space complexity correctly
  • Handle basic edge cases (empty input, single element)
  • Communicate your thought process while coding
Key Topics to Cover
Hash maps and frequency counting
Binary search and divide and conquer
Time and space complexity analysis
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 parallelize this solution?
  • How would you modify your solution to handle streaming input?
  • How would your solution change if the input was sorted?
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

The problem requires converting a matrix representation of a graph into an adjacency list. Each cell in the input matrix indicates whether there is an edge between two nodes (1 for an edge, 0 for no e...

Approach
  1. Initialize an empty list called adjacency_list to store the neighbors for each node.
  2. Loop through each row of the matrix. The index of the row represents the current node.
  3. For each element ...

Submit Your Answer
Markdown supported

Related Questions