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
Explain the difference between Depth-First Search (DFS) and Breadth-First Search (BFS): their typical uses, time and space complexity, order of node discovery, and real-world scenarios when you would pick one over the other (e.g., shortest unweighted paths, topological ordering, connectivity checks).
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
Discuss algorithms to compute Minimum Spanning Tree on very large graphs or in parallel/distributed settings. Compare Kruskal, Prim, Boruvka, and hybrid approaches, including how to parallelize edge sorting and component contraction. Mention handling negative weights and multiple components.
EasyTechnical
0 practiced
Write a recursive DFS function in Python for traversing a graph represented as an adjacency list. The implementation should correctly handle disconnected graphs and avoid revisiting nodes. As a software engineer, explain common pitfalls such as missing visited tracking, infinite recursion on cycles, and recursion depth limits; propose mitigations for deep graphs.
EasyTechnical
0 practiced
Given n nodes labeled 1..n and a list of undirected edges, implement an algorithm to count connected components. Provide both a DFS/BFS approach and a union-find approach in pseudocode or your chosen language. Discuss time/space trade-offs and when union-find is preferable.

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.