InterviewStack.io LogoInterviewStack.io

Trees & Graphs Basics Questions

Understand binary trees, binary search trees, and basic graph concepts. Know tree traversal methods: in-order, pre-order, post-order, and level-order (BFS). Practice DFS and BFS implementations. Know the difference between directed and undirected graphs. Solve medium-difficulty tree and graph problems.

MediumTechnical
27 practiced
Implement `iterative_postorder(root)` in Python that returns the post-order traversal of a binary tree without recursion using only one explicit stack. Explain the algorithm, how you detect when to process a node, and how to handle skewed trees and edge cases such as single-node trees.
EasyTechnical
27 practiced
Explain the difference between directed and undirected graphs. Give concrete ML engineering examples for each (for example: web link graphs and citation graphs as directed; user-item bipartite graphs as undirected/bidirectional). Discuss how directionality affects reachability, connectivity, and algorithms like PageRank, topological sort, and shortest path.
EasyTechnical
21 practiced
Implement two functions `dfs_recursive(graph, start)` and `dfs_iterative(graph, start)` in Python to return a list of nodes visited starting from `start`. The graph is provided as an adjacency list dict `{node: [neighbors...]}`. Ensure you correctly handle cycles, disconnected nodes, and large graphs where recursion depth might be an issue in production.
EasyTechnical
24 practiced
Implement two functions in Python: `inorder_recursive(root)` and `inorder_iterative(root)`. Each should return a list of node values in in-order. Assume the node class is: `class Node: def __init__(self, val, left=None, right=None): self.val = val; self.left = left; self.right = right`. For the iterative version avoid recursion and use an explicit stack. State time and space complexity and mention how you'd adapt the recursive version into a lazy generator for streaming.
HardTechnical
41 practiced
Design an approach to implement parallel BFS-like neighbor sampling on GPUs for large graphs used during GNN training. Explain memory layout (CSR on GPU), batching strategy, warp/coalesced memory accesses, handling variable-degree nodes (high-degree skew), and strategies to avoid memory over-subscription. Provide high-level pseudocode for a sampling kernel and discuss trade-offs.

Unlock Full Question Bank

Get access to hundreds of Trees & Graphs Basics interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.