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.

HardTechnical
0 practiced
Implement Heavy-Light Decomposition (HLD) on a tree to support path queries (e.g., sum on path u-v) with point updates. Provide preprocessing to compute heavy child, head, position arrays and implement query(u, v) and update(node, value) using a segment tree built over the base array. Explain complexity, when HLD is useful, and show Python code sketch for decomposition and query logic.
MediumTechnical
0 practiced
Implement prim_mst(n, adj) in Python using adjacency list and a min-heap to compute the MST total weight for a connected undirected weighted graph. adj is dict mapping node -> list of (neighbor, weight). Explain differences with Kruskal and when Prim is preferable (dense vs sparse).
MediumTechnical
0 practiced
Given an m x n grid where 0 is empty and 1 is an obstacle, implement shortest_path_grid(grid) in Python to return the minimum number of steps from top-left (0,0) to bottom-right (m-1,n-1) moving in 4 directions. If unreachable return -1. Explain complexity and how to handle large grids and memory constraints.
EasyTechnical
0 practiced
Implement level_order_nary(root) in Python for an n-ary tree where each node has a list of children. Return a list of lists where each inner list contains node values at that depth (level-order traversal). Describe time and space complexity and explain where the queue size peaks.
MediumTechnical
0 practiced
Implement neighbor_sampler(adj_list, seeds, num_samples, depth) in Python as part of a GNN training pipeline. The function should perform uniform neighbor sampling depth-by-depth to generate the computation graph nodes for a mini-batch starting from seed nodes. Discuss tradeoffs between full-neighborhood aggregation and sampled training in terms of bias, variance, memory, and runtime.

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.