Place the Fewest Cameras to Monitor a Warehouse Aisle Tree

Last updated: November 29, 2026

Quick Overview

Aisles in a fulfillment center form a binary tree. A camera at a node covers that node, its parent and its children. Return the minimum number of cameras needed so every node is covered.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


Aisles in a fulfillment center form a binary tree. A camera at a node covers that node, its parent and its children. Return the minimum number of cameras needed so every node is covered.

A Hard tree DP question in Amazon's onsite style, framed around security coverage in an FC. Interviewers want a greedy or state-based argument that you can defend, not a lucky guess, and they will push on why placing at leaves is wrong.

What the Interviewer Expects
  • Argue that leaves should never hold cameras since their parent covers strictly more
  • Define three states per node: needs coverage, covered without camera, has camera
  • Combine child states in post-order
  • Handle the root separately when it ends up uncovered
  • Prove optimality informally with an exchange argument
Key Topics to Cover
binary trees
greedy
tree DP
post-order traversal
minimum dominating set
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How does the solution change if a camera covers nodes within distance 2?
  • What if each node had a different camera cost?
  • Can you return which nodes get cameras?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: root of a non-empty binary tree. Output: minimum number of nodes to select so every node is selected or adjacent to a selected node....

High-Level Design

Process bottom up. Each call returns a state for its subtree root: 0 means it is not covered and needs its parent to place a camera, 1 means it holds a camera, 2 means it is covered by a child camera ...


Submit Your Answer
Markdown supported