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
35 practiced
Describe strategies for managing visited state when traversing graphs to avoid cycles. Cover: visited sets, boolean arrays, on-stack markers for directed graphs, in-place marking (when possible), and memory-efficient representations (bitset, bloom filters). Explain trade-offs between correctness, memory, and false positives or modification of input when using approximate methods.
EasyTechnical
35 practiced
Implement depth-first search (DFS) for a directed graph both recursively and iteratively in Python. Input: adjacency list dict and a start node id. Output: list of nodes in the order first visited. The iterative version should use an explicit stack and avoid recursion depth limits. Explain time and space complexity and differences in traversal order between the two approaches.
MediumTechnical
28 practiced
Implement serialize and deserialize functions for a binary tree using level-order (BFS) representation. The serializer should produce a compact string format (eg. comma-separated values with null markers) and deserializer should rebuild the exact tree structure. Discuss how this format handles missing children and why BFS is convenient for networks and streaming.
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.
MediumTechnical
35 practiced
You have a very deep tree or a graph that could trigger Python recursion depth errors (depth > 10k). Describe and implement approaches to safely traverse such structures in production, including converting recursive DFS to iterative, using explicit stacks, tail-call elimination alternatives, and streaming traversal to reduce memory.

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.