Graph Algorithms Interview Course
- 38Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is a graph?
38 questions
- 1
What is a graph?
Model answer
A graph is a collection of nodes and edges, where nodes represent entities and edges represent connections between them.
Explanation
The course defines a graph as a collection of nodes (vertices) and edges (connections). Nodes are typically drawn as circles with data, and edges are the lines connecting them.
- 2
What is another term commonly used for a node in a graph?
Model answer
Vertex.
Explanation
In the course, it mentions that 'vertex' is another word for node, though the instructor uses 'node' throughout.
- 3
What is an edge in a graph?
Model answer
An edge is a connection between a pair of nodes.
Explanation
Edges represent relationships between nodes; they are drawn as lines or arrows connecting two nodes.
- 4
What is a directed graph?
Model answer
A directed graph has arrowheads on its edges, meaning travel is only allowed in the direction of the arrow.
Explanation
In a directed graph, edges have direction; you can only traverse from one node to another following the arrow. The course contrasts this with an undirected graph where edges have no direction (two-way streets).
- 5
What is an undirected graph?
Model answer
An undirected graph has no arrowheads on its edges, meaning travel is allowed in both directions.
Explanation
Undirected edges represent bidirectional connections; you can move from either node to the other. The course likens this to a two-way street.
- 6
In a directed graph, what are neighbor nodes of a given node?
Model answer
Neighbor nodes are any nodes accessible from the current node through an outgoing edge, obeying the direction.
Explanation
Neighbors are defined by the arrows: from node A, you can only reach nodes that A points to. For example, if A points to B and C, then B and C are neighbors of A, but A is not necessarily a neighbor of C.
- 7
What is an adjacency list?
Model answer
An adjacency list is a hash map (or dictionary) where each key is a node and its value is an array of that node's neighbors.
Explanation
The course uses an adjacency list as a programmatic representation of a graph. In JavaScript, this is an object; in Python, a dictionary. It provides constant-time lookup of a node's neighbors.
- 8
How should a node with no neighbors appear in an adjacency list?
Model answer
It should still appear as a key in the adjacency list with an empty array as its value.
Explanation
Even if a node has no outgoing edges, it must be included as a key so that the graph knows the node exists. For example, node D in the course example has an empty neighbor array.
- 9
What data structure is used for a depth-first traversal of a graph?
Model answer
A stack.
Explanation
Depth-first traversal relies on a stack (Last-In-First-Out). In iterative implementations, you push and pop from the same end. Recursively, the call stack is used.
- 10
What data structure is used for a breadth-first traversal of a graph?
Model answer
A queue.
Explanation
Breadth-first traversal uses a queue (First-In-First-Out). You add nodes to the back and remove from the front.
- 11
Describe the order of node visitation in a depth-first traversal.
Model answer
Depth-first traversal explores as far as possible in one direction before backtracking and switching directions.
Explanation
From a starting node, it picks one neighbor and goes deep until it reaches a dead end, then backtracks to try other branches. The course illustrates this by exploring a single path fully before lateral moves.
- 12
Describe the order of node visitation in a breadth-first traversal.
Model answer
Breadth-first traversal explores all neighbors at the current depth before moving to nodes at the next depth.
Explanation
It starts at the source, visits all nodes one edge away, then all nodes two edges away, and so on. The course shows this as exploring in concentric circles.
- 13
How can depth-first traversal be implemented iteratively?
Model answer
By using an explicit stack data structure: initialize the stack with the starting node, then repeatedly pop the top, process it, and push its neighbors onto the stack.
Explanation
The iterative DFS uses a stack (e.g., JavaScript array with push/pop). The algorithm continues while the stack is not empty.
- 14
How can depth-first traversal be implemented recursively?
Model answer
By writing a recursive function that processes the current node and then calls itself on each neighbor, relying on the call stack.
Explanation
Recursive DFS uses the call stack implicitly. For each neighbor, it makes a recursive call. If a node has no neighbors, no recursive calls are made, which acts as an implicit base case.
- 15
Why can't breadth-first traversal be implemented recursively?
Model answer
Because recursion uses a stack (LIFO), which gives depth-first order, not the queue (FIFO) order needed for breadth-first.
Explanation
Breadth-first requires a queue to ensure nodes are processed in order of increasing distance. Recursion inherently uses a stack, which would mimic depth-first behavior.
- 16
What is the key difference in the iterative implementations of DFS and BFS?
Model answer
DFS uses a stack (pop from the same end used for push), while BFS uses a queue (shift from the front, push to the back).
Explanation
The algorithms are almost identical except for the data structure. DFS uses pop (LIFO) and BFS uses shift (FIFO), both with push to add neighbors.
- 17
What is the 'has path' problem?
Model answer
Given a directed acyclic graph (DAG) and a source and destination node, determine whether there is a path from the source to the destination.
Explanation
The problem asks to return true if a path exists, false otherwise. The graph is directed and acyclic, so no cycles exist.
- 18
For the has path problem on a directed acyclic graph, why is a visited set not necessary?
Model answer
Because the graph is acyclic, there are no cycles, so you can never revisit a node during traversal.
Explanation
In a DAG, once you leave a node, you cannot return to it via a different path, so you won't get stuck in an infinite loop. Therefore, you don't need to track visited nodes.
- 19
How do you convert an edge list into an adjacency list for an undirected graph?
Model answer
For each edge [a, b], add b to a's neighbor array and a to b's neighbor array, ensuring symmetry.
Explanation
Since the graph is undirected, every connection is bidirectional. The conversion iterates over edges, initializes both nodes as keys if not present, and pushes each node into the other's neighbor list.
- 20
Why is a visited set necessary when traversing an undirected graph?
Model answer
To prevent infinite loops caused by cycles, which are inherent in undirected graphs.
Explanation
In undirected graphs, you can travel back and forth between two nodes (a cycle). A visited set ensures each node is processed only once, avoiding infinite traversal.
- 21
How do you count the number of connected components in an undirected graph?
Model answer
Iterate over all nodes; for each unvisited node, start a traversal (DFS or BFS) that visits all nodes in that component, mark them visited, and increment a counter.
Explanation
The algorithm uses a loop to visit every node. If a node is not yet visited, it initiates a traversal to explore the entire component, incrementing the component count. Visited nodes are skipped.
- 22
How do you find the size of a connected component during a traversal?
Model answer
Start with a size of 1 for the current node, then recursively add the sizes from each neighbor's traversal (or iteratively count nodes visited).
Explanation
In the largest component problem, the recursive DFS returns 1 for the current node and sums the return values from recursive calls on neighbors, accumulating the total number of nodes in the component.
- 23
How do you find the largest component in an undirected graph?
Model answer
Iterate over all nodes, for each unvisited node, compute the size of its component using DFS, and track the maximum size encountered.
Explanation
Similar to counting components, but instead of incrementing a counter, you keep a variable for the largest size and update it when a component's size is larger.
- 24
Which traversal algorithm is best for finding the shortest path in an unweighted graph, and why?
Model answer
Breadth-first search (BFS) because it explores nodes in order of their distance from the source, so the first time the target is reached is guaranteed to be the shortest path (in terms of number of edges).
Explanation
BFS processes all nodes at distance k before moving to distance k+1. Therefore, when the target is first encountered, it is at the smallest possible distance. DFS could take a longer route due to its depth-first nature.
- 25
How do you track distance during BFS for finding the shortest path?
Model answer
Initialize a queue with a pair (source node, distance 0). When processing a node, for each neighbor, enqueue (neighbor, current distance + 1).
Explanation
Each element in the queue is a tuple of node and its distance from the source. The source starts at 0. When a neighbor is added, its distance is one more than the current node's distance.
- 26
In the shortest path problem, what should you return if no path exists between the source and destination?
Model answer
Return -1 (or a specified sentinel value).
Explanation
If the BFS completes without finding the destination, it means there is no path. The course uses -1 as the return value in such cases.
- 27
Why does BFS find the shortest path in an unweighted graph?
Model answer
Because BFS explores nodes in order of increasing distance from the source, so the first time the destination is visited, it is at the smallest possible distance.
Explanation
BFS processes nodes level by level. The distance of a node when it is first discovered is the number of edges on the shortest path from the source. This is proven by the level-order property of BFS.
- 28
What is a grid graph?
Model answer
A graph where each position (row, column) in a grid is a node, and edges connect adjacent positions vertically and horizontally.
Explanation
In grid graph problems like island count, the grid cells are treated as nodes. Two nodes are connected if they are adjacent up, down, left, or right.
- 29
What are the four possible neighbors of a grid position (r, c)?
Model answer
Up: (r-1, c), Down: (r+1, c), Left: (r, c-1), Right: (r, c+1).
Explanation
The course defines the neighbors by adding/subtracting 1 from the row or column index. The top-left is (0,0).
- 30
How do you count the number of islands in a grid?
Model answer
Iterate over all cells; for each unvisited land cell ('L'), perform a DFS to mark all connected land cells as visited, and increment a counter.
Explanation
This is analogous to counting connected components in a graph. The DFS explores all reachable land cells from the starting cell. Each new island starts a new traversal.
- 31
How do you find the minimum island size in a grid?
Model answer
During the traversal for each island, compute its size (number of cells) and track the minimum size among all islands.
Explanation
Similar to largest component but minimize. The DFS returns the size of the island, and you compare it with the current minimum, updating if smaller.
- 32
Why should you use a string like 'row,col' for the visited set in JavaScript when solving grid graph problems?
Model answer
Because arrays are reference types; two separate arrays with the same values are not considered equal by Set.has(). Strings are primitive and compared by value.
Explanation
In JavaScript, using an array like [r,c] in a Set will not work for lookups because each array literal creates a new reference. Converting to a string ensures consistent equality checks.
- 33
What are the typical base cases for a DFS on a grid?
Model answer
Out-of-bounds (row or column outside grid), current cell is water ('W'), or current cell is already visited.
Explanation
These base cases prevent illegal moves, stop at water, and avoid cycles. The order matters: check bounds before accessing the grid; check water; check visited.
- 34
What is the time complexity of a graph traversal that visits all nodes and edges?
Model answer
O(n + e) where n is the number of nodes and e is the number of edges, or O(e) since e dominates in connected graphs.
Explanation
Each edge is traversed at most once (in each direction for undirected), and each node is processed once. So the time is proportional to the number of edges, often written as O(e).
- 35
What is the space complexity of a graph traversal using a stack or queue?
Model answer
O(n) in the worst case, where n is the number of nodes.
Explanation
In the worst case (e.g., a long chain), the stack or queue may hold all nodes. The visited set also uses O(n) space.
- 36
In the worst case, how many edges can a graph with n nodes have?
Model answer
The number of edges can be up to O(n^2) (e.g., a complete graph).
Explanation
The course notes that in the worst case, n^2 edges are possible (for a directed graph with all possible connections). For undirected, it is n(n-1)/2, still O(n^2).
- 37
If you define n as the number of nodes, what is the worst-case time complexity for a graph traversal?
Model answer
O(n^2) because the number of edges can be O(n^2) in the worst case.
Explanation
When analyzing solely with n, the worst-case graph has about n^2 edges, so traversing all edges takes O(n^2) time.
- 38
What is the space complexity for the visited set in terms of nodes?
Model answer
O(n) because in the worst case, all nodes are added to the set.
Explanation
The visited set stores each node once, so its space is proportional to the number of nodes.

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