Graphs are non-linear data structures that consist of vertices (or nodes) connected by edges. They can represent complex real-world systems like network topology, social networks, or web pages, and therefore often feature in coding interviews. Candidates’ understanding of this structure, its traversal algorithms like Depth-First Search and Breadth-First Search, along with knowledge of pathfinding or graph theory algorithms, play a crucial role during a tech interview process.
Introduction to Graphs
- 1.
What is a Graph?
Answer:A graph is a data structure that represents a collection of interconnected nodes through a set of edges.
This abstract structure is highly versatile and finds applications in various domains, from social network analysis to computer networking.
Core Components
A graph consists of two main components:
- Nodes: Also called vertices, these are the fundamental units that hold data.
- Edges: These are the connections between nodes, and they can be either directed or undirected.
Visual Representation

Graph Representations
There are several ways to represent graphs in computer memory, with the most common ones being adjacency matrix, adjacency list, and edge list.
Adjacency Matrix
In an adjacency matrix, a 2D Boolean array indicates the edges between nodes. A value of
Trueat index[i][j]means that an edge exists between nodesiandj.Here is the Python code:
graph = [ [False, True, True], [True, False, True], [True, True, False] ]Adjacency List
An adjacency list represents each node as a list, and the indices of the list are the nodes. Each node’s list contains the nodes that it is directly connected to.
Here is the Python code:
graph = { 0: [1, 2], 1: [0, 2], 2: [0, 1] }Edge List
An edge list is a simple list of tuples, where each tuple represents an edge between two nodes.
Here is the Python code:
graph = [(0, 1), (0, 2), (1, 2)] - 2.
What are some common Types and Categories of Graphs?
Answer: - 3.
What is the difference between a Tree and a Graph?
Answer: - 4.
How can you determine the Minimum number of edges for a graph to remain connected?
Answer: - 5.
Define Euler Path and Euler Circuit in the context of graph theory.
Answer:
Graph Representations
- 6.
Compare Adjacency Lists and Adjacency Matrices for graph representation.
Answer: - 7.
What is an Incidence Matrix, and when would you use it?
Answer: - 8.
Discuss Edge List as a graph representation and its use cases.
Answer: - 9.
Explain how to save space while storing a graph using Compressed Sparse Row (CSR).
Answer:
Graph Traversal Algorithms
- 10.
Explain the Breadth-First Search (BFS) traversing method.
Answer: - 11.
Explain the Depth-First Search (DFS) algorithm.
Answer: - 12.
What are the key differences between BFS and DFS?
Answer: - 13.
Implement a method to check if there is a Path between two vertices in a graph.
Answer: - 14.
Solve the problem of printing all Paths from a source to destination in a Directed Graph with BFS or DFS.
Answer:
Graph Properties and Types
- 15.
What is a Bipartite Graph? How to detect one?
Answer: