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
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
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
- 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.
- 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.
- Upgrades on nodes you do not select never change the score, so only the chosen nodes' upgrade counts matter.