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.

EasyTechnical
21 practiced
Implement iterative pre-order traversal of a binary tree in C++ without recursion. Signature: vector<int> preorderTraversal(TreeNode* root). Explain why a stack is sufficient for preorder and analyze time and space complexity. Example: input [1,null,2,3] should produce [1,2,3].
MediumTechnical
19 practiced
Implement a function in Python to invert a binary tree in-place (swap left and right children of every node). Signature: def invertTree(root) -> TreeNode. Provide a concise recursive solution and an iterative solution using a stack or queue, and discuss complexity and when iterative might be preferred.
HardSystem Design
26 practiced
Design a friend-recommendation system for a large social network with 100 million active users and billions of follower/friend edges. Describe the data model for the social graph, storage choices (distributed adjacency lists, graph databases, or key-value stores), algorithms for recommendations (common neighbors, two-hop suggestions, personalized scoring), offline vs online computation, caching strategies, privacy concerns, and how to handle rate limits and scalability for real-time suggestions.
EasyTechnical
20 practiced
Explain the difference between a general binary tree and a binary search tree (BST). Include precise definitions, structural and ordering invariants, typical operations (search, insert, delete) with average and worst-case time complexity, example node arrangements (for example: [5,3,7] as a small BST and [5,4,3] as a skewed tree), and describe one real-world use case where an ordinary BST is not suitable without balancing.
HardTechnical
21 practiced
Implement an algorithm in C++ to compute the diameter (length of the longest path between any two nodes) of an unrooted tree. Signature: int diameterOfBinaryTree(TreeNode* root). Explain and implement both the two-BFS (or two-DFS) method and a single-pass DFS that computes heights and updates the diameter, and analyze time and space complexity.

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.