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 function to compute the number of connected components in an undirected graph given as an edge list in Python. Additionally, show how you'd use union-find to solve the same problem and compare performance for sparse and dense graphs. Mention memory and time trade-offs.
MediumTechnical
0 practiced
Implement an algorithm to convert a directed graph into its strongly connected components (SCCs) using Kosaraju's algorithm or Tarjan's SCC algorithm in Python. Return SCCs as lists of nodes. Explain runtime differences and describe situations in AI workflows where SCC detection helps (e.g., deduplicating equivalent states in MDPs).
MediumTechnical
0 practiced
Explain recursion depth issues when using DFS on very deep graphs (or skewed trees) in Python. Provide strategies to mitigate recursion limits and stack overflow: iterative DFS, tail recursion elimination (where supported), increasing recursion limit, and converting recursion to an explicit stack. Discuss trade-offs for production AI code running on servers with limited stack memory.
HardTechnical
0 practiced
Implement an algorithm to find the shortest cycle (girth) in an unweighted undirected graph. Provide a Python solution that runs in O(V * (V + E)) worst-case using BFS from every vertex, and discuss optimizations for sparse graphs. Explain use-cases where short cycle detection is meaningful in AI (e.g., anomaly detection in knowledge graphs).
MediumTechnical
0 practiced
Implement an algorithm to find all nodes reachable from a set of sources within exactly k edges in a directed graph (not undirected) and return them in sorted order. Use Python and ensure correctness on disconnected components and graphs with cycles. Explain performance implications when k is large relative to graph diameter.

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.