Question 1
EasyWhat is a graph?
- A.
A chart of numbers
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
What is a graph?
A chart of numbers
A sorted array
A set of vertices connected by edges
A tree with one child per node
Answer: C
Graphs model networks such as roads, friendships and web links.
Which representation uses a V × V matrix to store edges?
Adjacency matrix
Incidence list
Edge list
Adjacency list
Answer: A
It takes O(V²) space but answers 'is there an edge u–v?' in O(1).
Which representation is more space-efficient for a sparse graph?
Adjacency matrix
Neither
Both are equal
Adjacency list
Answer: D
An adjacency list uses O(V + E) space.
What is the degree of a vertex in an undirected graph?
Its distance from the root
The number of vertices in the graph
The number of edges incident on it
Its weight
Answer: C
The sum of all degrees equals 2E, since each edge counts twice.
Which traversal is used to find the shortest path (fewest edges) in an unweighted graph?
Depth-first search
Breadth-first search
Binary search
Inorder traversal
Answer: B
BFS reaches vertices in order of their distance from the source.
What is a cycle in a graph?
A vertex with no edges
A path that starts and ends at the same vertex
An edge with a weight
A graph with one vertex
Answer: B
A graph without cycles is acyclic; a connected acyclic undirected graph is a tree.
What is the output of this code?
g = {0: [1, 2], 1: [3], 2: [3], 3: [4], 4: []}
seen, order = set(), []
def dfs(u):
seen.add(u)
order.append(u)
for v in g[u]:
if v not in seen:
dfs(v)
dfs(0)
print(order)
[0, 1, 3, 4, 2]
[0, 2, 3, 4, 1]
Answer: A
DFS goes as deep as possible first: 0 → 1 → 3 → 4, then backtracks to visit 2.
What is a directed graph?
A graph drawn with arrows only for decoration
A graph with exactly one vertex
A graph whose edges have a direction, from one vertex to another
A graph with no edges
Answer: C
An edge u → v doesn't mean there is an edge v → u.
What is a weighted graph?
A very large graph
A graph whose vertices are heavy
A graph with no cycles
A graph whose edges carry values such as distance or cost
Answer: D
Road networks with distances are weighted graphs.
What is an adjacency list?
A list of all the vertices only
A sorted list of edge weights
For each vertex, a list of its neighbours
A V × V table of 0s and 1s
Answer: C
It uses O(V + E) space, which suits sparse graphs.
[4, 3, 1, 2, 0]
[0, 1, 2, 3, 4]