Quick Overview

A coding problem where node capacities can be raised by single-unit upgrades within a total budget, after which exactly k nodes must be selected to maximize the bitwise AND of their capacities. It tests bitwise reasoning, the cost of reaching a target bit pattern and efficient handling of inputs up to 10^5 elements.

Maximize the Bitwise AND of k Node Capacities Under an Upgrade Budget

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

A deployment uses `n` nodes, and `capacities[i]` is the capacity of node `i`. Before deploying you may upgrade nodes: one upgrade picks any index `i` and increases `capacities[i]` by 1. You may perform at most `upgrade_budget` upgrades in total, and the same node may be upgraded any number of times. After the upgrades you must select exactly `deployment_size` distinct nodes. The reliability score of a selection is the bitwise AND of the selected nodes' capacities after the upgrades. Return the maximum reliability score you can achieve. ### Function Signature ```python def maximize_deployment_reliability(capacities: list[int], upgrade_budget: int, deployment_size: int) -> int: ``` ### Rules - Each upgrade adds exactly 1 to one node's capacity. Capacities never decrease. - Upgrades may go to any nodes, including nodes you do not select, and you do not have to use the whole budget. - The selection contains exactly `deployment_size` distinct indices. Its score is the bitwise AND of their capacities after all upgrades. - Return the maximum score as a single integer. When `deployment_size == 1`, the score is the capacity of the one selected node. ### Constraints - `n == len(capacities)` - `1 <= deployment_size <= n <= 10^5` - `0 <= upgrade_budget <= 10^9` - `0 <= capacities[i] <= 10^9` - No capacity can exceed `max(capacities) + upgrade_budget <= 2 * 10^9` after upgrades, so the answer fits in a signed 32-bit integer. ### Examples #### Example 1 ```text Input: capacities = [5,4,1,7,2], upgrade_budget = 3, deployment_size = 3 Output: 6 ``` Upgrade node 0 once (5 → 6) and node 1 twice (4 → 6), then select the capacities 6, 6 and 7. Their AND is 6 (`110` in binary). No selection of three nodes scores above 6 within 3 upgrades. #### Example 2 ```text Input: capacities = [5,4,3,2], upgrade_budget = 1, deployment_size = 2 Output: 5 ``` Upgrade node 1 once (4 → 5) and select the two nodes with capacity 5. Their AND is 5. #### Example 3 ```text Input: capacities = [1,2,4,8,16], upgrade_budget = 10, deployment_size = 3 Output: 8 ``` Upgrade node 1 six times (2 → 8) and node 2 four times (4 → 8), using the whole budget, then select the three nodes with capacity 8. Their AND is 8.

Overview: A coding problem where node capacities can be raised by single-unit upgrades within a total budget, after which exactly k nodes must be selected to maximize the bitwise AND of their capacities. It tests bitwise reasoning, the cost of reaching a target bit pattern and efficient handling of inputs up to 10^5 elements.

Read the full Amazon Software Engineer interview experience this question came from

A deployment uses `n` nodes, and `capacities[i]` is the capacity of node `i`. Before deploying you may upgrade nodes: one upgrade picks any index `i` and increases `capacities[i]` by 1. You may perform at most `upgrade_budget` upgrades in total, and the same node may be upgraded any number of times. After the upgrades you must select exactly `deployment_size` distinct nodes. The reliability score of a selection is the bitwise AND of the selected nodes' capacities after the upgrades. Return the maximum reliability score you can achieve. ### Rules - Each upgrade adds exactly 1 to one node's capacity. Capacities never decrease. - Upgrades may go to any nodes, including nodes you do not select, and you do not have to use the whole budget. - The selection contains exactly `deployment_size` distinct indices. Its score is the bitwise AND of their capacities after all upgrades. - Return the maximum score as a single integer. When `deployment_size == 1`, the score is the capacity of the one selected node. ### Example 1 ```text Input: capacities = [5,4,1,7,2], upgrade_budget = 3, deployment_size = 3 Output: 6 ``` Upgrade node 0 once (5 to 6) and node 1 twice (4 to 6), then select the capacities 6, 6 and 7. Their AND is 6 (`110` in binary). No selection of three nodes scores above 6 within 3 upgrades. ### Example 2 ```text Input: capacities = [5,4,3,2], upgrade_budget = 1, deployment_size = 2 Output: 5 ``` Upgrade node 1 once (4 to 5) and select the two nodes with capacity 5. Their AND is 5. ### Constraints - `n == len(capacities)` - `1 <= deployment_size <= n <= 10^5` - `0 <= upgrade_budget <= 10^9` - `0 <= capacities[i] <= 10^9` - No capacity can exceed `max(capacities) + upgrade_budget <= 2 * 10^9` after upgrades, so every input value and the answer fit in a signed 32-bit integer (the answer never exceeds 2^31-1).

Constraints

  • n == len(capacities)
  • 1 <= deployment_size <= n <= 10^5
  • 0 <= upgrade_budget <= 10^9
  • 0 <= capacities[i] <= 10^9
  • No capacity can exceed max(capacities) + upgrade_budget <= 2 * 10^9 after upgrades, so the answer fits in a signed 32-bit integer.

Examples

Input: ([5, 4, 1, 7, 2], 3, 3)

Expected Output: 6

Explanation: Example 1: raise 5 and 4 to 6 with all 3 upgrades, and 6 AND 6 AND 7 is 6.

Input: ([5, 4, 3, 2], 1, 2)

Expected Output: 5

Explanation: Example 2: one upgrade turns 4 into 5, and 5 AND 5 is 5.

Hints

  1. For the AND of a group to contain a given mask, every selected node must end with all bits of that mask set. Work out the fewest upgrades a single node needs for that.
  2. Adding 1 can carry: raising 7 until bit 3 turns on gives 8 and clears the lower bits. Check what that does to lower bits you also wanted set.
  3. Upgrades on nodes you do not select never change the score, so only the chosen nodes' upgrade counts matter.

Maximum Bitwise AND After Limited Node Upgrades

hardAmazonSoftware Engineer

A deployment uses n nodes, and capacities[i] is the capacity of node i. Before deploying you may upgrade nodes: one upgrade picks any index i and increases capacities[i] by 1. You may perform at most upgrade_budget upgrades in total, and the same node may be upgraded any number of times.

After the upgrades you must select exactly deployment_size distinct nodes. The reliability score of a selection is the bitwise AND of the selected nodes' capacities after the upgrades. Return the maximum reliability score you can achieve.

Rules

  • Each upgrade adds exactly 1 to one node's capacity. Capacities never decrease.
  • Upgrades may go to any nodes, including nodes you do not select, and you do not have to use the whole budget.
  • The selection contains exactly deployment_size distinct indices. Its score is the bitwise AND of their capacities after all upgrades.
  • Return the maximum score as a single integer. When deployment_size == 1, the score is the capacity of the one selected node.

Example 1

Input:  capacities = [5,4,1,7,2], upgrade_budget = 3, deployment_size = 3
Output: 6

Upgrade node 0 once (5 to 6) and node 1 twice (4 to 6), then select the capacities 6, 6 and 7. Their AND is 6 (110 in binary). No selection of three nodes scores above 6 within 3 upgrades.

Example 2

Input:  capacities = [5,4,3,2], upgrade_budget = 1, deployment_size = 2
Output: 5

Upgrade node 1 once (4 to 5) and select the two nodes with capacity 5. Their AND is 5.

Constraints

  • n == len(capacities)
  • 1 <= deployment_size <= n <= 10^5
  • 0 <= upgrade_budget <= 10^9
  • 0 <= capacities[i] <= 10^9
  • No capacity can exceed max(capacities) + upgrade_budget <= 2 * 10^9 after upgrades, so every input value and the answer fit in a signed 32-bit integer (the answer never exceeds 2^31-1).

Examples

Example 1
Input
([5, 4, 1, 7, 2], 3, 3)
Output
6
Notes
Example 1: raise 5 and 4 to 6 with all 3 upgrades, and 6 AND 6 AND 7 is 6.
Example 2
Input
([5, 4, 3, 2], 1, 2)
Output
5
Notes
Example 2: one upgrade turns 4 into 5, and 5 AND 5 is 5.

Constraints

  • •n == len(capacities)
  • •1 <= deployment_size <= n <= 10^5
  • •0 <= upgrade_budget <= 10^9
  • •0 <= capacities[i] <= 10^9
  • •No capacity can exceed max(capacities) + upgrade_budget <= 2 * 10^9 after upgrades, so the answer fits in a signed 32-bit integer.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def maximize_deployment_reliability(capacities: list[int], upgrade_budget: int, deployment_size: int) -> int:
Sample call
maximize_deployment_reliability([5, 4, 1, 7, 2], 3, 3)
Language
Case 1
Input
([5, 4, 1, 7, 2], 3, 3)
Expected
6
Case 2
Input
([5, 4, 3, 2], 1, 2)
Expected
5

Browse More Questions

Show the approach

Approach

Call a mask T reachable when some deployment_size nodes can each be raised to a value containing every bit of T with at most upgrade_budget upgrades in total. The best score equals the largest reachable mask. The AND of any achievable selection is itself reachable, and a selection that makes T reachable has an AND containing T, so its score is at least T.

Every submask of a reachable mask is reachable, so the answer is built one bit at a time from bit 30 down to bit 0 (no capacity can pass 2 * 10^9, which is below 2^31). Keep the best mask so far in answer and, for each bit b, keep target = answer | (1 << b) when it is reachable. Invariant: answer holds exactly the bits of the optimum A above the current bit. If A has bit b, target is a submask of A and is reachable. If A lacks bit b, a reachable target would be larger than A, which is impossible. The loop therefore ends with answer == A.

Reachability splits per node. For capacity c, the cheapest raised value is the smallest v >= c containing target. If c already contains target, v = c. Otherwise let j be the highest bit of target missing from c and set v = (((c >> j) | 1) << j) | target: keep the bits of c above j, set bit j, clear the bits below j and add back the low bits of target. Any valid v first differs from c at some bit p where v has a 1 and c has a 0. If p < j, bit j of v is still clear; if p > j, v is larger than the formula's value. So p = j, and the smallest low part is exactly the low bits of target. Raising a node to bit j wipes out its low bits, which is why each target's cost is computed from the original capacity; adding up separate per-bit costs would undercount. The node's cost is v - c. Sort the costs; target is reachable exactly when the deployment_size smallest costs sum to at most upgrade_budget. Upgrades on unselected nodes never help, and leftover budget may stay unused.

Edge cases: deployment_size == 1 yields max(capacities) + upgrade_budget, deployment_size == n forces every node into the selection, and upgrade_budget == 0 gives the best AND of deployment_size original capacities. One cost can approach 2^31 and the sum of up to 10^5 costs can reach about 2.1 * 10^14, so the sums use 64-bit integers (long in Java, long long in C++); JavaScript numbers stay exact below 2^53.

Time complexity:
O(B * n log n) with B = 31 bit positions
Space complexity:
O(n)