Commonly Asked Data Structure Interview Questions on Graph (original) (raw)

Last Updated : 6 Sep, 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.

graph-data-structure

Graphs

Theoretical Questions for Interviews on Graph

1. What are the types of graphs?

Graphs can be classified based on various characteristics:

2. 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.

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.

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?

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 the difference between a directed graph and an undirected graph, and why does this affect traversal 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:

**Disadvantages:

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