Maximize Protected City Population
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's April 2026 OA includes a greedy placement problem where you position security units to maximize the population they protect. You have n cities in a line, each with a population value. Units start at specific cities and can move left once or stay put. The trick is deciding which cities to protect given the unit constraints. If you blank on the greedy strategy during the live OA, StealthCoder will feed you the solution in real time so you don't tank the submission.
The problem
You are given n cities arranged in a line. City i has population population[i] and may contain a security unit described by unit[i], where unit[i] = '1' means a unit is initially stationed in city i. Each security unit may stay where it is, or if it is not in the first city, it may move exactly one city to the left. Every unit can move at most once. After all moves are chosen, a city is protected if at least one security unit is stationed there. Return the maximum total population of all protected cities. Function Description Complete the function maximizeProtectedPopulation in the editor below. maximizeProtectedPopulation has the following parameters: Returns long: the maximum total population of protected cities. Move the unit from city 2 to city 1, keep the unit in city 3, and keep the unit in city 5. Cities 1, 3, and 5 are then protected, for a total population of 10 + 8 + 9 = 27. Move the only unit left from city 2 to city 1. Protecting city 1 yields the larger total population.
Reported by candidates. Source: FastPrep
Pattern and pitfall
This is a greedy problem disguised as dynamic programming. The key insight: process cities left to right and decide whether each unit should stay or move left. At each position, you want to protect the city with the highest population that the unit can reach (current city or left neighbor). The common pitfall is thinking you need to try all combinations, which leads to exponential time. Instead, use a greedy choice at each unit: if moving left protects a higher-population city that isn't already protected, move. Otherwise, stay. This greedy approach works because moving left is irreversible and units can't move right, so committing to a protection decision early never blocks a better solution later. StealthCoder will catch the greedy pattern and give you the exact implementation if you freeze mid-OA.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Maximize Protected City Population cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Maximize Protected City Population FAQ
Is this a DP problem or greedy?+
Greedy. Process units left to right and make locally optimal choices about which city each unit protects. DP would be overkill and time out. Greedy works because units can't undo moves or move right, so early decisions don't block better future choices.
What's the trick with the move-left constraint?+
A unit at city i can protect city i or city i-1, never both. So the decision at each unit is binary: stay and protect my current city, or move left and protect the previous city. Choose based on which has higher population and isn't already protected.
How do I handle already-protected cities?+
Track which cities are protected in a set or boolean array. When a unit decides to move left, only move if the left neighbor isn't already protected and has higher population than staying. If the current city is already protected, the unit should move left if the left city has higher population.
Can I solve this in one pass?+
Yes. Iterate through units left to right. At each unit, compare the population of its current city and its left neighbor (if it exists). Protect the higher-population city that isn't already protected. Mark cities as protected and sum their populations at the end.
What's the time complexity I should target?+
O(n) where n is the number of cities. One pass through the units, greedy decision at each step. Space is O(n) for tracking protected cities. Amazon expects linear time for this problem, so anything worse means you're overcomplicating it.