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
30 practiced
Explain how you manage visited state during graph traversal to avoid infinite loops in cyclic graphs. Provide a short discussion for (a) DFS recursion, (b) DFS iterative, and (c) BFS. In addition, explain the subtle difference between marking visited when you push/enqueue a node vs when you pop/dequeue it and the consequences for multi-edge and directed graphs.
EasyTechnical
33 practiced
Compare adjacency list and adjacency matrix representations for graphs. For a graph with n nodes and m edges, describe memory and time complexity for common operations (iterate neighbors, check edge existence, add/remove edges). As a data engineer designing storage for graphs with billions of edges, explain when you would use compressed formats like CSR/CSC or edge lists and how sparsity/density affects the choice.
MediumTechnical
29 practiced
Given an edge list for an undirected graph, implement connected components using Union-Find (disjoint set union) in Python. Provide def count_components(edges: List[Tuple[int,int]]) -> int that returns the number of components. Use path compression and union by rank and discuss time complexity on large datasets.
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.
MediumTechnical
39 practiced
Implement Kosaraju's algorithm to compute strongly connected components (SCCs) of a directed graph in Python. Provide def kosaraju_scc(graph) -> List[List[int]] where graph is adjacency list. Discuss complexity and memory usage and mention iterative vs recursive implementations for deep 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.