Alien Defense Turret Placement
Updated
Problem statement
The binary matrix grid represents an alien-defense zone. A value of 1 is a house and 0 is an empty cell.
You may place one turret on any cell, including a cell containing a house. A turret at (r, c) protects every house at (hr, hc) whose Manhattan distance satisfies |r - hr| + |c - hc| <= k.
Return the maximum number of houses that one turret can protect.
Function
maxProtectedHouses(grid: int[][], k: int) → intExamples
Example 1
grid = [[1,0,0],[0,1,0],[0,0,1]]k = 2return = 3A turret at the center protects all three diagonal houses because each is within Manhattan distance two.
Example 2
grid = [[1,0,1,0,1]]k = 1return = 2A turret on the empty cell between two neighboring houses is distance one from both, so it protects two houses.
Example 3
grid = [[0,0],[0,0]]k = 5return = 0The zone contains no houses.
Constraints
1 <= grid.length <= 200.1 <= grid[i].length <= 200, and all rows have the same length.grid[i][j]is0or1.0 <= k <= 400.
More Bloomberg LP Onsite Interview questions
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent WordsONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Decode StringPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Minimum Replacements to Make Two Strings AnagramsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Range Sum Queries on a Fixed GridGoogle · ONSITE INTERVIEWGoogle interview questions
- Centered K-by-K Submatrix SumsCitadel · ONSITE INTERVIEWCitadel interview questions
- Restaurant Delivery HeatmapDoorDash · ONSITE INTERVIEWDoorDash interview questions
- Matrix Region SumRippling · OA · ONSITE INTERVIEWRippling interview questions
- Prefix Matrix Products and AutogradOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Number of Submatrices That Sum to TargetWhatNot · ONSITE INTERVIEWWhatNot interview questions
- Split a Matrix Into Four Partitions by Average (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Shortest Path Around Rectangular ObstaclesDRW · OADRW interview questions
- Sub-matrix SumsIMC Trading · OAIMC Trading interview questions
- Largest Sub-GridMathWorks · OAMathWorks interview questions