Graph Algorithms Interview Course
- 26Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What are the two fundamental components of a graph?
What are the two fundamental components of a graph?
26 questions
- 1
What are the two fundamental components of a graph?
- A
Vertices and paths
- B
Nodes and adjacency lists
- C
Edges and weights
- D
Nodes and edges
Correct answer
DNodes and edges
Explanation
A graph is defined as a collection of nodes (vertices) and edges (connections between nodes).
- A
- 2
In a directed graph, what does an arrowhead on an edge indicate?
- A
The direction of travel is restricted to the arrow's direction
- B
The edge can be traversed in both directions
- C
The edge is weighted
- D
The edge is part of a cycle
Correct answer
AThe direction of travel is restricted to the arrow's direction
Explanation
In a directed graph, arrowheads indicate the allowed direction of movement along the edge.
- A
- 3
What data structure is typically used to represent a graph programmatically in JavaScript?
- A
A binary tree
- B
An adjacency list (object or dictionary)
- C
A linked list
- D
A 2D array
Correct answer
BAn adjacency list (object or dictionary)
Explanation
An adjacency list, often implemented as a hash map (JavaScript object), is the preferred way to represent graph information.
- A
- 4
In an adjacency list, what should be included for a node that has no outgoing edges?
- A
The node should be omitted from the adjacency list
- B
The node should be represented only if it has at least one edge
- C
The node should still appear as a key with an empty array
- D
The node should appear as a key with a null value
Correct answer
CThe node should still appear as a key with an empty array
Explanation
Even if a node has no neighbors, it should still appear as a key in the adjacency list with an empty array to indicate its existence.
- A
- 5
What is a neighbor node in a graph?
- A
Any node within the same graph component
- B
A node that is part of a cycle
- C
A node that shares an edge with the current node, obeying edge direction
- D
A node that is directly connected to another node with no consideration of direction
Correct answer
CA node that shares an edge with the current node, obeying edge direction
Explanation
A neighbor node is any node accessible through an edge, respecting the direction of the edge.
- A
- 6
Which data structure is essential for implementing a depth-first traversal iteratively?
- A
Stack
- B
Hash set
- C
Priority queue
- D
Queue
Correct answer
AStack
Explanation
A depth-first traversal uses a stack (LIFO) to process nodes in a deep-first order.
- A
- 7
What is the key difference between depth-first and breadth-first traversal orders?
- A
Depth-first cannot be implemented recursively; breadth-first can
- B
Depth-first goes as deep as possible in one direction before switching; breadth-first explores all neighbors of a node before going deeper
- C
Depth-first explores all directions evenly; breadth-first explores one direction deeply
- D
Depth-first uses a queue; breadth-first uses a stack
Correct answer
BDepth-first goes as deep as possible in one direction before switching; breadth-first explores all neighbors of a node before going deeper
Explanation
Depth-first pursues a single path deeply before backtracking, while breadth-first explores all immediate neighbors first.
- A
- 8
Can a breadth-first traversal be implemented recursively?
- A
Yes, using the call stack as a queue
- B
Yes, but only for trees, not graphs
- C
Yes, by reversing the order of recursive calls
- D
No, because recursion inherently uses a stack, which contradicts the queue order required for BFS
Correct answer
DNo, because recursion inherently uses a stack, which contradicts the queue order required for BFS
Explanation
BFS requires a queue (FIFO) order, but recursion uses a stack (LIFO), making recursive BFS impractical.
- A
- 9
In the recursive depth-first traversal implementation shown in the course, what serves as the implicit base case?
- A
An if statement checking if the node is null
- B
A node with no outgoing edges (empty neighbor list) causes no recursive calls
- C
A visited set prevents infinite recursion
- D
The function returns when the call stack is full
Correct answer
BA node with no outgoing edges (empty neighbor list) causes no recursive calls
Explanation
When a node has no neighbors, the for loop iterates zero times, so no recursive calls are made, acting as a base case.
- A
- 10
In the has path problem for a directed acyclic graph, why is a visited set not necessary?
- A
Because DFS only visits each node once anyway
- B
Because the graph is guaranteed to have no cycles, so there is no risk of infinite loops
- C
Because the graph is directed, preventing back-and-forth movement
- D
Because the recursion automatically handles repetitions
Correct answer
BBecause the graph is guaranteed to have no cycles, so there is no risk of infinite loops
Explanation
The problem states the graph is acyclic, meaning no cycles exist, so traversal will never revisit a node through a cycle.
- A
- 11
In the DFS solution for has path, what is the base case?
- A
If the source node is visited, return false
- B
If the graph is empty, return false
- C
If the source node equals the destination node, return true
- D
If the source node has no neighbors, return false
Correct answer
CIf the source node equals the destination node, return true
Explanation
The base case checks if the current source node is the destination; if so, a path is found and true is returned.
- A
- 12
In the undirected path problem, why must an edge list be converted to an adjacency list?
- A
Because edge lists are only for directed graphs
- B
Because the problem requires it
- C
Because traversal algorithms are designed to work with adjacency lists for efficient neighbor access
- D
Because edge lists cannot be used in any traversal algorithm
Correct answer
CBecause traversal algorithms are designed to work with adjacency lists for efficient neighbor access
Explanation
Traversal algorithms like DFS and BFS require quick access to neighbors, which an adjacency list provides via constant-time lookup.
- A
- 13
When converting an undirected edge list to an adjacency list, what must be ensured?
- A
Each edge is added only once in the adjacency list
- B
The adjacency list is stored as a 2D array
- C
Each edge is represented symmetrically: if A is a neighbor of B, then B is also a neighbor of A
- D
Only the smaller node is used as a key
Correct answer
CEach edge is represented symmetrically: if A is a neighbor of B, then B is also a neighbor of A
Explanation
For undirected graphs, the adjacency list must be symmetric because edges are bidirectional.
- A
- 14
In the undirected path problem, what is used to prevent infinite loops due to cycles?
- A
A special marker on edges
- B
A visited set to track nodes already explored
- C
A counter to limit recursion depth
- D
A maximum number of iterations
Correct answer
BA visited set to track nodes already explored
Explanation
A visited set ensures each node is processed only once, preventing cycles from causing infinite traversal.
- A
- 15
For the connected components count problem, what is the purpose of the iterative loop over all nodes?
- A
To initialize the visited set
- B
To ensure every node is visited exactly once
- C
To start a new traversal on each connected component by checking if a node is unvisited
- D
To count the number of edges
Correct answer
CTo start a new traversal on each connected component by checking if a node is unvisited
Explanation
The loop iterates through all nodes; when an unvisited node is found, a traversal explores its entire component, and the count increments.
- A
- 16
In the largest component problem, how is the size of a component calculated during traversal?
- A
By returning 1 for the current node and adding the sizes returned from recursive calls on neighbors
- B
By counting the number of edges traversed
- C
By incrementing a global counter each time a node is visited
- D
By using a separate queue to store sizes
Correct answer
ABy returning 1 for the current node and adding the sizes returned from recursive calls on neighbors
Explanation
Each recursive call returns the size of the subtree, starting with 1 for the current node, and sums the sizes from neighbors.
- A
- 17
Why is breadth-first search preferred over depth-first search for finding the shortest path in an unweighted graph?
- A
Because DFS might explore a long path before finding a shorter one, while BFS explores all paths evenly and finds the shortest first
- B
Because DFS cannot handle cycles
- C
Because BFS is always faster in any graph
- D
Because BFS uses less memory
Correct answer
ABecause DFS might explore a long path before finding a shorter one, while BFS explores all paths evenly and finds the shortest first
Explanation
BFS explores nodes in order of distance from the start, so the first time the destination is reached, it is via the shortest path.
- A
- 18
In the shortest path algorithm, what information is stored with each node in the queue?
- A
Only the node identifier
- B
The node identifier and the path taken so far
- C
The node identifier and its distance from the start node
- D
The node identifier and its parent node
Correct answer
CThe node identifier and its distance from the start node
Explanation
Each queue entry is a pair: the node and its distance from the starting node, allowing easy distance tracking.
- A
- 19
If no path exists between two nodes in the shortest path problem, what should be returned?
- A
0
- B
null
- C
undefined
- D
-1
Correct answer
D-1
Explanation
The problem specification states that -1 should be returned if there is no path between the given nodes.
- A
- 20
In a grid graph, what are the possible neighbors of a cell at (r, c)?
- A
Up: (r+1, c), Down: (r-1, c), Left: (r, c+1), Right: (r, c-1)
- B
Only up and down
- C
All eight directions including diagonals
- D
Up: (r-1, c), Down: (r+1, c), Left: (r, c-1), Right: (r, c+1)
Correct answer
DUp: (r-1, c), Down: (r+1, c), Left: (r, c-1), Right: (r, c+1)
Explanation
In a grid graph, movement is typically allowed only in four cardinal directions: up, down, left, right.
- A
- 21
In the island count problem, what is the purpose of the visited set?
- A
To differentiate between land and water
- B
To track which islands have been fully explored
- C
To prevent counting the same piece of land more than once and to avoid infinite loops
- D
To store the size of each island
Correct answer
CTo prevent counting the same piece of land more than once and to avoid infinite loops
Explanation
As in any undirected graph traversal, a visited set prevents revisiting nodes and infinite cycles.
- A
- 22
Why is a string like "r,c" used as a key in the visited set for grid problems?
- A
Because it is faster than using an array
- B
Because strings are easier to concatenate
- C
Because the problem requires string input
- D
Because JavaScript sets use reference equality for objects, and strings are primitive and uniquely identify positions
Correct answer
DBecause JavaScript sets use reference equality for objects, and strings are primitive and uniquely identify positions
Explanation
Using strings avoids reference equality issues; separate array objects would be considered different even with same values.
- A
- 23
In the minimum island problem, what initial value is typically used for the minimum size variable?
- A
1
- B
Infinity
- C
-1
- D
0
Correct answer
BInfinity
Explanation
Positive infinity ensures that any valid island size (at least 1) will replace it and the comparison works correctly.
- A
- 24
What is the time complexity of a standard graph traversal (DFS or BFS) if n is the number of nodes and e is the number of edges?
- A
O(n^2)
- B
O(n * e)
- C
O(n)
- D
O(e)
Correct answer
DO(e)
Explanation
In the worst case, each edge is traversed once, so the time complexity is O(e). (Alternatively, O(n+e) but often simplified to O(e) when e > n.)
- A
- 25
What is the space complexity of a DFS or BFS traversal when using a set to store visited nodes?
- A
O(n)
- B
O(e)
- C
O(1)
- D
O(log n)
Correct answer
AO(n)
Explanation
In the worst case, all nodes must be stored in the visited set and also on the stack/queue, giving O(n) space.
- A
- 26
For a grid graph with R rows and C columns, what is the worst-case time complexity of an algorithm that visits every cell?
- A
O(R^2)
- B
O(R + C)
- C
O(R * C)
- D
O(C^2)
Correct answer
CO(R * C)
Explanation
Every cell may be visited, so time complexity is proportional to the total number of cells, R * C.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.