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
71 practiced
Implement a function in Python to detect a cycle in a directed graph using DFS. The graph is given as an adjacency list. Return True if any cycle exists and False otherwise. Explain why a simple visited set is insufficient for directed cycle detection and provide time/space complexity.
EasyTechnical
80 practiced
Write a function to check whether a binary tree is height-balanced (AVL-style): for every node, heights of left and right subtrees differ by at most 1. Implement an O(n) recursive solution in Python that returns a boolean. Discuss how balancing affects tree operations in AI feature stores and decision-tree inference.
HardTechnical
74 practiced
Given two nodes in a rooted tree, implement an algorithm to compute their Lowest Common Ancestor (LCA). Provide both: (1) a simple O(h) solution using parent pointers and depth, and (2) an O(1) query solution after O(n log n) preprocessing using binary lifting. Implement binary lifting preprocessing and query in Python and explain memory/time trade-offs.
MediumTechnical
85 practiced
Design and implement union-find (disjoint set union) with path compression and union by rank in Python. Provide methods: make_set(x), find(x), union(x,y). Explain amortized complexity and a scenario in AI data preprocessing where union-find accelerates operations (e.g., clustering or deduplicating connected components).
HardTechnical
64 practiced
Given two rooted trees representing hierarchical taxonomies, design an algorithm to compute a minimum edit sequence (node moves, renames) to transform one taxonomy into the other. Discuss approximate and exact approaches, tree edit distance algorithms, and complexity. Explain how this applies to merging label ontologies across datasets in AI projects.

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.