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
27 practiced
Explain visited-state management in graph traversals. Describe the differences between marking nodes visited on discovery (enqueue) versus when they are processed (dequeue), or using color states (white/gray/black). Discuss implications for correctness, duplicate work, cycle detection, multi-source traversal, and parallel traversals in SRE systems.
EasyTechnical
34 practiced
Implement in Python a function shortest_path(adj: Dict[int, List[int]], start: int, target: int) -> List[int] that returns the shortest path (list of node ids) from start to target in an unweighted directed graph using BFS. If no path exists return an empty list. The graph is given as an adjacency list (nodes labeled 0..n-1). Aim for O(V+E) time and include path reconstruction. Example: adj = {0:[1,2],1:[2],2:[3],3:[]} start=0 target=3 => [0,2,3].
MediumTechnical
48 practiced
You must traverse a very large filesystem tree to find the top-k directories by cumulative size. Describe an algorithm and provide pseudocode (or Python sketch) for a streaming approach that uses O(k + h) memory (h = traversal stack depth). Explain how you handle symlink cycles, permission errors, and filesystems that change during traversal.
EasyTechnical
31 practiced
Explain breadth-first search (BFS) and depth-first search (DFS). Describe their algorithmic differences, the typical data structures used (queue vs stack/recursion), and practical SRE scenarios where you'd prefer one over the other (e.g., shortest unweighted path, dependency-depth analysis, reachability checks). Compare their time and space complexity and mention limitations for extremely deep or very wide graphs.
HardTechnical
34 practiced
Given multiple traversal logs produced by different traversals of a static binary tree (some logs might be incomplete/missing nodes), design an algorithm to determine whether there exists a binary tree consistent with all logs. Logs can be partial (missing subtrees). Describe how to model constraints induced by traversals and how to check consistency efficiently.

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.