InterviewStack.io LogoInterviewStack.io

Tree and Graph Traversal Questions

Comprehensive mastery of tree and graph traversal algorithms, representations, and common interview problem patterns. Understand graph models and representation choices including adjacency lists versus adjacency matrices and trade offs based on sparsity and density, as well as properties such as directed versus undirected and weighted versus unweighted edges. Know visited state management to avoid cycles and techniques for cycle detection. Implement breadth first search and depth first search in both recursive and iterative forms, understand when to use a queue versus a stack, and analyze time and space complexity. Apply traversals to problems such as shortest path in unweighted graphs, connected component detection, topological sort for dependency ordering, cycle detection, path existence, and island counting. For trees, master traversal orders including in order, pre order, post order, and level order with both recursive and iterative implementations, including explicit stack based approaches and constant space approaches where relevant. Practice tree specific problems such as lowest common ancestor, path sum, tree serialization and deserialization, validating binary search trees, balancing and reconstruction of trees from traversal sequences, and converting between tree and graph formulations. Emphasize clean code, correctness, handling edge cases such as empty or skewed structures, recursion base cases and depth limits, and explaining trade offs between recursion and iterative solutions with respect to performance and memory.

EasyTechnical
28 practiced
Compare adjacency list vs adjacency matrix representations. For each representation provide memory complexity, common operations (iterate neighbors, edge lookup), and discuss trade-offs for sparse vs dense graphs and typical machine learning applications (GNN input, similarity matrices, embedding graphs).
HardTechnical
34 practiced
You need to convert recursive tree-traversal model code into a vectorized approach for batch inference across many trees (e.g., in a batch of XGBoost models). Describe techniques to transform traversal into SIMD-friendly operations or table lookups and discuss memory layout and cache effects.
MediumTechnical
38 practiced
Implement iterative postorder traversal of a binary tree using one stack (no recursion) in Python and return values in postorder. This is trickier than preorder/inorder—provide a clear algorithm and discuss edge cases like single-node and skewed trees.
EasyTechnical
28 practiced
Implement a function `shortest_path(graph, start, goal)` in Python where `graph` is an adjacency-list dict mapping node ids to lists of neighbor ids. Return the shortest path as a list of nodes (including start and goal) or `None` if unreachable. Constraints: O(V+E) time, O(V) extra memory. Handle empty graphs and start==goal.
MediumTechnical
30 practiced
Implement an iterative DFS in Python to check whether there exists a path between two nodes in a directed graph. Use early exit to improve performance, handle missing nodes gracefully, and discuss stack size and worst-case complexity.

Unlock Full Question Bank

Get access to hundreds of Tree and Graph Traversal interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.