InterviewStack.io LogoInterviewStack.io

Trees and Graphs Questions

Comprehensive knowledge of tree and graph data structures and algorithms commonly tested in coding interviews. Candidates should understand representations such as adjacency list and adjacency matrix and when to use each, and tree representations including n ary trees and binary search trees. Expect to implement and reason about traversals including depth first search and breadth first search, tree traversals such as pre order in order and post order, and level order traversal. Cover algorithms including topological sorting for directed acyclic graphs, cycle detection, connected components, shortest path algorithms such as breadth first search for unweighted graphs, Dijkstra for nonnegative weights, and Bellman Ford for graphs with negative edges, and minimum spanning tree algorithms such as Kruskal and Prim. Include disjoint set union find for connectivity and for use with Kruskal, lowest common ancestor techniques and implementations, tree dynamic programming problems, serialization and deserialization, reconstruction from traversals, balancing and validation checks for binary search trees and balanced tree concepts, diameter and path sum problems, and common interview patterns such as path finding dependency resolution and structural transformation. Emphasize implementation details and common pitfalls including correct use of visited tracking recursion depth edge cases and disconnected components, and practice articulating time and space complexity tradeoffs and algorithm selection under different constraints.

MediumTechnical
59 practiced
Explain and implement the Bellman-Ford algorithm in Python to compute shortest paths from a source in a graph that may contain negative edge weights. Signature: def bellman_ford(edges: List[Tuple[int,int,int]], n: int, source: int) -> Tuple[List[float], bool] where the boolean indicates whether a negative-weight cycle is present. Discuss why Bellman-Ford is O(VE) and when it's necessary over Dijkstra.
MediumTechnical
60 practiced
Given preorder and inorder traversal arrays of a binary tree, implement a function in Python to reconstruct the original binary tree. Signature: def build_tree(preorder: List[int], inorder: List[int]) -> TreeNode. Discuss algorithmic complexity and edge cases such as mismatched input sizes or repeated values.
EasyTechnical
75 practiced
Implement a Union-Find (Disjoint Set Union) data structure in C++ with path compression and union by rank. Provide functions: int find(int x) and void unite(int x, int y). Explain time complexity for m operations on n elements and describe a backend use-case where union-find is preferable to repeated DFS/BFS.
EasyTechnical
72 practiced
Explain the main practical differences between BFS and DFS in terms of order of visiting nodes, memory patterns, and when one is preferred over the other for backend tasks such as computing shortest unweighted paths, exploring configuration graphs, or performing garbage collection/traversal.
HardSystem Design
85 practiced
Design a graph query service (system design) to compute shortest-path queries on a continental road network for a backend API used by routing clients. Requirements: low-latency queries (under 200ms), support many concurrent queries, memory budget on each machine limited to a few GBs, ability to update edge weights periodically (traffic updates). Describe high-level architecture, partitioning/sharding strategy, preprocessing (e.g., contraction hierarchies, landmarks), caching, fallback strategies, and how you would measure and test performance.

Unlock Full Question Bank

Get access to hundreds of Trees and Graphs interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.