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.

MediumTechnical
0 practiced
Design an algorithm to reconstruct a binary tree from its preorder and inorder traversal lists. Implement in Python and analyze time and space complexity. Then explain how similar reconstruction techniques are used in parsing expression trees for compilers or converting parse trees into linearized sequences for transformer training.
HardTechnical
0 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.
HardTechnical
0 practiced
For an AI system that learns from graph-structured data, you need to compute personalized PageRank for hundreds of seed nodes efficiently. Describe algorithms and optimizations (approximate PageRank, push-based algorithms, power iteration with sparsity) to compute or approximate personalized PageRank vectors. Explain space/time trade-offs and when approximation is acceptable.
EasyTechnical
0 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.
MediumTechnical
0 practiced
Explain Bellman-Ford algorithm: when it's needed, its time complexity, and how it detects negative cycles. Provide a concise high-level pseudocode and then describe a case in reasoning over knowledge graphs where negative edges (conceptual negatives) or cycle detection might matter.

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.