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.

MediumTechnical
27 practiced
Given preorder and inorder traversal arrays of a binary tree with unique values, implement buildTree(preorder, inorder) in C++ that reconstructs the binary tree and returns its root. Discuss time and space complexity and how to optimize lookups to O(1) using auxiliary data structures.
MediumTechnical
57 practiced
Implement stronglyConnectedComponents(graph) in Java to compute all strongly connected components (SCCs) for a directed graph. You may use Kosaraju's or Tarjan's algorithm—choose one and justify your choice. Explain complexity, memory trade-offs, and how to validate correctness on graphs with many small SCCs.
EasyTechnical
37 practiced
Explain the formal differences between a tree and a general graph. Describe properties that define a tree (connected, acyclic, exactly n-1 edges for n nodes), implications such as unique simple path between nodes, and how those properties simplify algorithms (e.g., no need for visited set in some traversals). Give concrete examples of when you'd model a problem as a tree versus as a general graph.
EasyTechnical
38 practiced
Write a Python function count_components(n, edges) that returns the number of connected components in an undirected graph with n nodes labeled 0..n-1 given an edge list. Implement using DFS (iterative or recursive) and ensure isolated nodes are counted. State time and space complexity and behavior for very sparse graphs.
HardTechnical
59 practiced
Implement Bellman-Ford in C++ to compute single-source shortest paths on graphs that may have negative edge weights but no negative cycles. Detect negative cycles and explain practical optimizations and alternatives (SPFA heuristics, Johnson's algorithm for all-pairs) when V*E complexity is prohibitive for large 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.