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
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).
MediumTechnical
0 practiced
Count the number of islands in a 2D grid of 0s and 1s (classic islands problem). Implement either DFS or BFS flood-fill in your chosen language. Discuss boundary checks, visited marking, diagonal adjacency optionality, and memory implications for very large grids.
MediumTechnical
0 practiced
Given an undirected graph and a small integer k, describe and implement a backtracking algorithm to determine whether the graph is k-colorable. Discuss complexity and heuristics you would use in production to handle larger graphs (node ordering, DSATUR, greedy heuristics).
HardTechnical
0 practiced
Given a tree where each node has an integer value, find the subtree (contiguous nodes under some node) that has the maximum average value and return the root of that subtree. Implement an O(n) solution using tree DP that computes sum and count for each subtree and tracks the maximum average. Discuss numerical stability and tie-breaking.
HardTechnical
0 practiced
Implement Lowest Common Ancestor queries using binary lifting for large constraints but focus on memory optimization and low constant factors. Provide C++ code skeleton or explanation to reduce memory footprint for up to 200k nodes and 200k queries and discuss trade-offs.

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.