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.

HardSystem Design
0 practiced
Design the friend-suggestion feature for a social network that relies on graph structure. Describe offline feature computation (common neighbors, Jaccard, embeddings), real-time ranking, caching, graph storage choices, privacy considerations, and how to evaluate suggestion quality. Address scale to hundreds of millions of users.
HardTechnical
0 practiced
Describe Yen's algorithm for computing the K shortest simple paths between a source and target in a directed weighted graph. Explain the subroutine reliance on shortest-path algorithm, handling of path deviations, complexity and memory characteristics, and pitfalls with repeated nodes and cycles.
EasyTechnical
1 practiced
Explain the trade-offs between using an adjacency list and an adjacency matrix to represent graphs. As a software engineer, describe memory and runtime complexity for common operations (iterate neighbors, check edge existence, add/remove edge), give concrete guidance for when to choose each representation for sparse vs dense graphs, and mention hybrid/compressed representations (e.g., CSR, bitsets) and their use cases.
EasyTechnical
0 practiced
Implement a Disjoint Set Union (Union-Find) data structure with path compression and union by rank/size supporting find, union, and connected operations. Provide complexity analysis and discuss practical details for production code (e.g., iterative find, recursion depth, memory layout).
HardTechnical
0 practiced
Given a directed acyclic graph (DAG) representing tasks with durations and precedence constraints, design an algorithm to compute the earliest completion time for each task and the overall project completion time. Explain how topological ordering and the critical path method are combined and how to modify the algorithm when resources are constrained (limited parallel workers).

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.