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
27 practiced
Given an undirected graph represented as an adjacency list, implement count_connected_components(graph) in Python that returns the number of connected components. Graph nodes can be any hashable type. Ensure you handle empty graphs and disconnected nodes efficiently using BFS or DFS.
EasyTechnical
32 practiced
Implement iterative inorder traversal of a binary tree in Python without recursion. The TreeNode structure has 'val', 'left', and 'right' attributes. Your function should return a list of node values in inorder. Ensure your solution handles empty trees and very skewed trees without causing recursion depth issues.
HardTechnical
36 practiced
You are building graph-query APIs used by frontend: getFriendsAtDistance(user, k), mutualConnections(u,v), and recommendConnections(user). For a massive social graph, design the traversal algorithms, caching/precomputation strategies (e.g., 1-hop and 2-hop caches, partial indexes), synchronous vs asynchronous execution trade-offs, TTL and consistency considerations, and monitoring metrics you would track to ensure latency and correctness.
HardTechnical
38 practiced
Design a compact serialization and streaming protocol for an N-ary tree that supports encoding and decoding in constant auxiliary memory proportional to encoding buffer (i.e., you cannot buffer the entire tree). Provide Python pseudocode for serialize and a streaming deserialize that can resume after partial reads. Discuss how to represent unknown child counts and how to handle backpressure when writing to the network.
HardTechnical
37 practiced
Implement a Python function that detects a cycle in a directed graph and, if a cycle exists, returns one cycle as a list of nodes in order. The graph is given as an adjacency list and may have multiple components and parallel edges. Your solution should run in O(V+E) time and handle self-loops properly. Explain how you reconstruct the cycle using the recursion stack or parent pointers.

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.