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.

EasyTechnical
0 practiced
Write count_connected_components(n, edges) in Python to return the number of connected components in an undirected graph with nodes 0..n-1. Explain complexity and which graph representation is preferable when the graph is sparse versus dense.
MediumTechnical
0 practiced
Write build_tree_pre_in(preorder, inorder) in Python to reconstruct a binary tree given preorder and inorder traversal arrays (assume unique values). Implement an O(n) solution using a hashmap for inorder indices and explain why using indices instead of slicing avoids extra memory/time overhead.
HardTechnical
0 practiced
Implement Yen's algorithm in Python to find the K shortest simple paths (no repeated nodes) from source to target in a directed weighted graph with non-negative weights. Provide an implementation strategy that generates candidate spur paths efficiently and explain how you prevent duplicate path outputs and manage complexity.
HardTechnical
0 practiced
Implement a persistent Disjoint Set Union (DSU) with rollback in Python to be used in divide-and-conquer-on-time offline algorithms (e.g., dynamic connectivity). Provide functions union(a,b), snapshot(), rollback(), and connected(a,b) and explain why path compression complicates rollback and how to design the DSU for persistence.
HardTechnical
0 practiced
Explain message-passing GNN layers and derive computational and memory complexity per layer for full-neighborhood aggregation. Provide a Python-like pseudo-code sketch of an optimized sparse aggregation using CSR format (iterating row pointers and scatter-add) and discuss numerical stability, batching, and memory tradeoffs.

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.