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
28 practiced
Design a distributed BFS that can run on a cluster to traverse a graph with billions of vertices and edges. Describe partitioning strategy, how you store partitions on workers, communication protocol to expand frontiers across partitions, techniques to reduce network I/O, fault tolerance, and how to measure scalability and bottlenecks. Provide sketches of data structures and message formats.
EasyTechnical
38 practiced
Implement breadth-first search (BFS) in Python. Input: adjacency list as a dict mapping node ids to lists of neighbor ids, and a start node id. Output: a tuple (order, dist) where order is list of nodes in visitation order by level and dist is a dict mapping node -> minimum edge distance from start. Your solution should run in O(n + m) time and handle disconnected nodes gracefully.
EasyTechnical
33 practiced
When should you use a queue (BFS) versus a stack (DFS) for graph or tree traversal? Provide concise rules and give at least three realistic AI-engineer scenarios where you would prefer BFS and three where DFS is more appropriate. Explain how these choices affect time to find a solution, memory usage, and ability to find shortest paths in unweighted graphs.
EasyTechnical
50 practiced
List and explain the four common binary tree traversals: preorder, inorder, postorder, and level-order. For each traversal describe a canonical use case from AI engineering or compiler/interpreter workloads, and state whether the traversal is naturally recursive or iterative and why.
HardTechnical
57 practiced
You receive a stream of edges describing an undirected graph too large to store fully in memory. Explain algorithms and sketches to maintain approximate connectivity information, estimate number of connected components, and detect approximate shortest paths or distances with limited memory. Mention techniques like semi-streaming model, union-find sketches, and probabilistic counters.

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.