Place the Fewest Monitoring Agents to Cover a Service Tree
Last updated: November 29, 2026
Quick Overview
An agent installed on a node in a binary tree of Azure services watches that node, its parent and its children. Return the minimum number of agents needed so every node is watched.
Microsoft
November 29, 20260
0
0 solved
An agent installed on a node in a binary tree of Azure services watches that node, its parent and its children. Return the minimum number of agents needed so every node is watched.
A greedy tree DP that Microsoft onsite interviewers use to tell strong candidates apart. They want three clear states, a justification for why leaves should never host an agent, and a post-order pass.
What the Interviewer Expects
- Define states: needs coverage, has an agent, covered without an agent
- Argue greedily that a leaf's parent is always at least as good as the leaf
- Process children before parents (post-order)
- Treat null children as covered
- Handle an uncovered root at the end
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
- Agents have different costs per node. Solve with a 3-state DP.
- An agent covers everything within distance 2.
- Prove the greedy is optimal with an exchange argument.
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 of a binary tree.
- Agent at v covers v, parent(v), children(v).
- Output: minimum number of agents....
High-Level Design
Work bottom up. Placing an agent on a leaf covers only the leaf and its parent, while placing it on the parent covers the leaf, the parent, its other child and the grandparent, so it is never worse. T...