Commonly Asked Data Structure Interview Questions on Graph
Last Updated :
03 Mar, 2025
A graph is a non-linear data structure that consists of a set of nodes (also known as vertices) connected by edges. Unlike trees, which have a hierarchical structure, graphs can represent more complex relationships, such as social networks, web pages, road maps, and more. This article contains the most asked interview questions and answers about data structures which will help you to prepare for your technical interviews.
Theoretical Questions for Interviews on Graph
1. What is a graph?
A graph is a data structure made up of nodes (vertices) and edges (connections between nodes). The nodes represent entities, and the edges represent the relationships between them. Graphs can represent real-world systems like networks, social media connections, and transportation systems.
2. What are the types of graphs?
Graphs can be classified based on various characteristics:
- Directed Graph (Digraph): A graph in which the edges have a direction, indicating a one-way relationship between nodes.
- Undirected Graph: A graph in which the edges do not have a direction, representing a bidirectional relationship.
- Weighted Graph: A graph in which edges have weights or costs associated with them, often used for pathfinding and optimization.
- Cyclic Graph: A graph that contains at least one cycle (a path that starts and ends at the same node).
- Connected Graph: A graph where there is a path between every pair of vertices.
3. What is the adjacency matrix representation of a graph?
An adjacency matrix is a 2D array used to represent a graph. For a graph with n vertices, the matrix is of size n x n, where each element at position [i][j] indicates the presence (or weight) of an edge between vertex i and vertex j.
- For an undirected graph, the matrix is symmetric.
- For a directed graph, the matrix is not necessarily symmetric.
- If the graph is weighted, the value at [i][j] is the weight of the edge.
4. What is the adjacency list representation of a graph?
An adjacency list is a collection of lists or arrays where each list corresponds to a vertex in the graph and contains the nodes that are directly connected to it by an edge. This representation is more space-efficient than an adjacency matrix, especially for sparse graphs.
5. What is the time complexity of accessing an edge in an adjacency matrix?
The time complexity for accessing an edge in an adjacency matrix is O(1), as you can directly access the element at position [i][j] to check if there is an edge between vertex i and vertex j.
6. What is the time complexity of accessing an edge in an adjacency list?
The time complexity for accessing an edge in an adjacency list is O(deg(v)), where deg(v) is the degree of vertex v (the number of edges connected to it). This is because you need to iterate through the list of adjacent nodes to check for a specific edge.
7. What is depth-first search (DFS)?
Depth-first search (DFS) is an algorithm used for traversing or searching a graph. It starts at the root (or an arbitrary node in the case of an undirected graph) and explores as far as possible along each branch before backtracking. DFS is implemented using either a recursive approach or a stack.
8. What is breadth-first search (BFS)?
Breadth-first search (BFS) is an algorithm used to traverse or search a graph in a level-order manner. It starts at the root and explores all its neighbors before moving on to the next level of neighbors. BFS is implemented using a queue.
9. What is the time complexity of DFS and BFS?
The time complexity of both depth-first search (DFS) and breadth-first search (BFS) is O(V + E), where V is the number of vertices and E is the number of edges in the graph. This is because both algorithms visit every vertex and edge once.
10. What is the difference between DFS and BFS?
- DFS explores as deep as possible along a branch before backtracking, using a stack or recursion. It is better for problems that involve exploring all possible paths, such as pathfinding or maze solving.
- BFS explores all vertices at the present depth level before moving on to the next level, using a queue. It is optimal for finding the shortest path in an unweighted graph.
11. How would you find the shortest path in an unweighted graph?
The shortest path in an unweighted graph can be found using breadth-first search (BFS). BFS guarantees the shortest path because it explores nodes level by level, ensuring that when a node is first visited, it is visited via the shortest possible path.
12. How would you find the shortest path in a weighted graph?
To find the shortest path in a weighted graph, Dijkstra's algorithm is commonly used. It maintains a set of nodes whose shortest distance from the source is known and iteratively explores the nearest unvisited node, updating the shortest distances of its neighbors.
13. What is a cycle in a graph?
A cycle in a graph is a path that starts and ends at the same vertex, with all edges in the cycle being distinct. In directed graphs, cycles are particularly significant, as they can affect the traversal or computation of graph algorithms.
14. What is topological sorting?
Topological sorting is the process of ordering the vertices of a directed acyclic graph (DAG) such that for every directed edge u → v, vertex u comes before v in the ordering. Topological sorting is used in tasks such as job scheduling and dependency resolution.
15. Why do we use dijkastra's algorithm and how is implemented?
Dijkstra's algorithm is used to find the shortest path in weighted graphs with non-negative edges. It initializes distances to infinity (except the source, which is 0), uses a priority queue to process nodes with the smallest tentative distance, and updates neighbors if a shorter path is found. The process repeats until all nodes are processed, ensuring the shortest path.
16. What is a spanning tree of a graph?
A spanning tree of a graph is a subgraph that includes all the vertices of the graph and is a tree (i.e., it has no cycles). A graph can have multiple spanning trees, and finding a minimum spanning tree (MST) is a common problem in graph theory.
17. What is Kruskal’s algorithm?
Kruskal’s algorithm is a greedy algorithm used to find the minimum spanning tree (MST) of a graph. It works by sorting all edges in increasing order of their weights and adding edges to the MST while ensuring no cycles are formed.
18. What is Prim’s algorithm?
Prim’s algorithm is another greedy algorithm used to find the minimum spanning tree (MST) of a graph. It starts with an arbitrary vertex and grows the MST by adding the nearest vertex that is not already in the tree, selecting the edge with the minimum weight.
19. What are the advantages and disadvantages of using a graph?
- Advantages:
- Can represent complex relationships between objects.
- Flexible, supporting both directed and undirected edges.
- Efficient algorithms for searching, pathfinding, and optimization.
- Disadvantages:
- Can be computationally expensive for large graphs.
- Requires more memory due to the need for storing nodes and edges.
- Some graph algorithms (e.g., finding all shortest paths) can be inefficient in dense graphs.
20. How would you detect a cycle in a directed graph?
To detect a cycle in a directed graph, you can use depth-first search (DFS) with an auxiliary array to track visited nodes. During DFS, if a node is revisited while still in the recursion stack (i.e., it is currently being explored), a cycle is detected.
Top Coding Interview Questions on Graph
The following list of 50 coding problems on Graphs covers a range of difficulty levels, from easy to hard, to help candidates prepare for interviews.
Top 50 Graph Coding Problems for Interviews
Similar Reads
Commonly Asked Data Structure Interview Questions
To excel in a Data Structure interview, a strong grasp of fundamental concepts is crucial. Data structures provide efficient ways to store, organize, and manipulate data, making them essential for solving complex problems in software development.Interviewers often test candidates on various data str
6 min read
Commonly Asked Data Structure Interview Questions on Stack
A stack follows the Last In, First Out (LIFO) principle, meaning the last element added is the first to be removed. Stacks are a fundamental data structure used in many real-world applications, including expression evaluation, function call management, and backtracking algorithms. Theoretical Questi
5 min read
Commonly Asked Data Structure Interview Questions on Matrix
A Matrix can be considered as array of arrays or 2D array. We use matrix data structure to store two dimensional data.Theoretical Questions for Interviews on Matrix1. What is the difference between row-major and column-major order in matrix representation? In row-major order, elements in the same ro
4 min read
Commonly Asked Data Structure Interview Questions on Sorting
Sorting is a fundamental concept in computer science and data structures, often tested in technical interviews. Sorting algorithms are essential for organizing data in a specific order, whether it's ascending or descending. Understanding various sorting techniquesâlike Quick Sort, Merge Sort, Bubble
4 min read
Commonly Asked Data Structure Interview Questions on Searching
Searching is a fundamental concept in computer science, involving the process of finding a specific element in a collection of data. Efficient searching techniques are crucial for optimizing performance, especially when dealing with large datasets. Theoretical Questions for Interviews on Searching1.
3 min read
Commonly Asked Data Structure Interview Questions on Backtracking
Backtracking is a powerful algorithmic technique used to solve problems where you need to explore all possible solutions and choose the best one. In data structure interviews, backtracking problems often involve recursively exploring different configurations, making it ideal for solving problems lik
3 min read
Commonly Asked Data Structure Interview Questions on Linked List
Unlike arrays, which are stored in contiguous memory locations, linked lists consist of nodes, where each node contains data and a reference (or link) to the next node in the sequence. This structure provides advantages in terms of dynamic size, easy insertion, and deletion of elementsTheoretical Qu
5 min read
Commonly Asked Interview Questions on Tree
A tree is a hierarchical data structure consisting of nodes, with each node having a value and references (or pointers) to its child nodes. The tree structure is used to represent relationships in various domains such as file systems, organizational structures, and decision trees. One of the primary
6 min read
Top 50 Problems on Hash Data Structure asked in SDE Interviews
Hashing is a technique or process of mapping keys, and values into the hash table by using a hash function. It is done for faster access to elements. The efficiency of mapping depends on the efficiency of the hash function used. To learn more about hashing and hashmaps, please refer to the Tutorial
3 min read
Commonly Asked Algorithm Interview Questions
For tech job interviews, knowing about algorithms is really important. Being good at algorithms shows you can solve problems effectively and make programs run faster. This article has lots of common interview questions about algorithms. Commonly Asked Algorithm Interview Questions Table of Content C
15+ min read