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.

HardSystem Design
36 practiced
Given a workflow DAG of 100k tasks with estimated runtimes per task and k parallel workers, design an algorithm to schedule tasks respecting dependencies to minimize expected overall completion time. Explain how you would combine topological ordering with scheduling heuristics (e.g., critical path, list scheduling) and how you'd handle tie-breaking, resource constraints, and dynamic task durations.
EasyTechnical
27 practiced
Explain the difference between breadth-first search (BFS) and depth-first search (DFS). In a data engineering context, describe which traversal you would choose for tasks such as (a) finding shortest path in an unweighted graph, (b) exploring dependency graphs to detect cycles, and (c) scanning a very large graph where memory is limited. Discuss typical data structures used, time and space complexity, and practical tradeoffs for production pipelines.
EasyTechnical
34 practiced
Implement level-order traversal (BFS) of a binary tree in Python. Given the class TreeNode with attributes val, left, right, implement def level_order(root): -> List[List[int]] that returns a list of levels where each level is a list of node values. Assume the tree may be empty and must run in O(n) time and O(w) extra space where w is max width.
EasyTechnical
35 practiced
Write a Python function def shortest_path_length_unweighted(graph, src, dst): -> int that returns the length (number of edges) of the shortest path between src and dst in an unweighted graph represented as adjacency list dict. Return -1 if no path exists. The graph can be large but fits in memory for this question.
HardTechnical
35 practiced
Design partitioning strategies for distributed graph processing to minimize cross-machine communication during iterative traversals like BFS or Label Propagation. Discuss edge-cut vs vertex-cut approaches, streaming partitioners vs offline tools like METIS, replication of high-degree nodes, and tradeoffs between balance and locality for power-law graphs.

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.