Compute tree depth and meeting rooms
Company: Zest
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates skills in tree traversal and recursion for computing maximum depth of a binary tree, alongside interval scheduling and resource allocation for determining the minimum number of meeting rooms, testing reasoning about data structures such as trees and interval endpoints.
Maximum Depth of a Binary Tree
Constraints
- 0 <= number of nodes <= 100,000
- Node values fit in a 32-bit integer
- The tree may be a long single chain (highly unbalanced)
- An empty tree returns 0
Examples
Input: ([3, 9, 20, None, None, 15, 7],)
Expected Output: 3
Explanation: Root 3 -> right child 20 -> leaf 15 (or 7): the longest path has 3 nodes.
Input: ([],)
Expected Output: 0
Explanation: Empty tree has depth 0.
Input: ([1],)
Expected Output: 1
Explanation: A single root node has depth 1.
Input: ([1, 2, None, 3, None, 4, None, 5],)
Expected Output: 5
Explanation: A left-leaning chain 1->2->3->4->5 has 5 nodes on its longest path.
Input: ([1, None, 2, None, 3],)
Expected Output: 3
Explanation: A right-leaning chain 1->2->3 has depth 3.
Input: ([1, 2, 3, 4, 5, 6, 7],)
Expected Output: 3
Explanation: A complete tree of 7 nodes has 3 levels.
Hints
- Maximum depth equals the number of levels in a breadth-first traversal of the tree.
- A recursive depth-first solution is concise, but with up to 100,000 nodes a degenerate chain can overflow the call stack — prefer an iterative BFS or an explicit stack.
- When the input is a level-order array, you can reconstruct parent-to-child links in one left-to-right pass using a queue of parents, consuming two array slots (left, right) per parent and skipping nulls.
Minimum Number of Meeting Rooms
Constraints
- 0 <= number of intervals <= 100,000
- intervals[i] = [start, end) with start < end
- All times are integers
- Intervals are half-open: a meeting ending at t does not conflict with one starting at t
Examples
Input: ([[0, 30], [5, 10], [15, 20]],)
Expected Output: 2
Explanation: [0,30) overlaps both [5,10) and [15,20), so 2 rooms are needed; [5,10) and [15,20) don't overlap each other.
Input: ([[7, 10], [10, 12]],)
Expected Output: 1
Explanation: Half-open: the meeting ending at 10 does not conflict with the one starting at 10, so one room suffices.
Input: ([],)
Expected Output: 0
Explanation: No meetings require no rooms.
Input: ([[1, 5]],)
Expected Output: 1
Explanation: A single meeting needs one room.
Input: ([[1, 10], [2, 7], [3, 19], [8, 12], [10, 20], [11, 30]],)
Expected Output: 4
Explanation: At time just after 11, meetings [3,19), [8,12), [10,20), [11,30) all overlap -> 4 rooms.
Input: ([[0, 5], [0, 5], [0, 5]],)
Expected Output: 3
Explanation: Three identical fully-overlapping meetings need 3 rooms.
Input: ([[1, 2], [2, 3], [3, 4], [4, 5]],)
Expected Output: 1
Explanation: Back-to-back half-open meetings chain through a single room.
Hints
- Decouple starts from ends: sort all start times and all end times into two separate sorted arrays.
- Sweep with two pointers. The maximum number of meetings that are simultaneously in progress is the answer.
- Because intervals are half-open, treat a start at time t as NON-conflicting with an end at time t — use a strict `start < end` comparison so the ending meeting frees its room first.