Fewest Monitoring Agents to Cover Every Node in a Tree Network
Last updated: November 29, 2026
Quick Overview
A binary tree models a network. An agent placed on a node monitors that node, its parent and its children. Return the minimum number of agents needed to monitor every node.
0
0
0 solved
A binary tree models a network. An agent placed on a node monitors that node, its parent and its children. Return the minimum number of agents needed to monitor every node.
A Google onsite hard problem in the style of placing probes in a hierarchical network. The interviewer expects the greedy three-state post-order solution and a justification of why placing at a leaf's parent is never worse than at the leaf.
What the Interviewer Expects
- Define three states: has agent, covered without agent, not covered
- Combine children's states in post-order
- Place an agent whenever a child is uncovered
- Handle the root being uncovered at the end
- Justify the greedy choice with an exchange argument
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Prove the greedy choice is optimal.
- What if agents cover distance 2 instead of 1?
- What if each node has a different placement cost?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: root. Output: minimum number of agents such that every node is either hosting an agent or adjacent to one. At least one node exists....
High-Level Design
Process bottom-up, giving each node one of three states: 0 = not covered, 1 = has an agent, 2 = covered by a child's agent with no agent here. Treat null children as covered (state 2) so leaves are no...