← Amazon Interview Insights

Amazon·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
May 2026

Summary

Amazon coding question, greedy/DP flavored, the kind that looks like a simple array problem until you actually try to implement it.

Questions Asked (1)

Q1

You have n cities in a row, each with a population value. Some cities start with a safety unit, some don't. Each safety unit can either stay in place or move one step left, and can only be moved once. A city is considered protected if it ends up with at least one safety unit. Find the optimal set of moves to maximize the total population of protected cities.

Algorithms & Data Structures
Author's notes

Spent the first few minutes just re-reading the constraints because I kept second-guessing whether units could move right too.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as an optimization over cities with safety units, where each unit can cover itself or the city to its left. Use dynamic programming to decide for each unit whether to move left or stay, maximizing the sum of populations of covered cities. Consider edge cases such as multiple units covering the same city and cities with no units.

Pro tip: Clarify that each unit can move at most once and only left, and that a city is protected if at least one unit ends there. Then, discuss how to handle overlapping coverage efficiently, perhaps by precomputing prefix sums or using a greedy approach after sorting units.

1. Understand the problem and constraints

Restate the problem: n cities in a row, each with a population and possibly a safety unit. Each unit can stay or move one step left, at most once. Goal: maximize total population of cities that end with at least one unit.

2. Identify the decision for each unit

For each city with a unit, decide whether to keep the unit there or move it left. This decision affects coverage of the current city and the left neighbor.

3. Formulate a dynamic programming state

Define DP state based on the current city index and whether the previous city is already covered by a unit from the right. This captures the dependency between adjacent cities.

4. Define transitions and base cases

At each city, if there is a unit, consider moving it left (covering current and left) or staying (covering current only). If no unit, coverage depends on whether a unit from the right moved left. Maximize population sum.

5. Optimize and analyze complexity

The DP runs in O(n) time and O(1) or O(n) space. Discuss potential greedy simplifications or edge cases like consecutive units.

Key Points to Mention

  • Dynamic programming with state representing coverage of the previous city.
  • Each unit's move is binary: stay or move left, affecting at most two cities.
  • Overlapping coverage: multiple units can cover the same city, but only one is needed.
  • Edge cases: cities with no units, consecutive units, units at the first city (cannot move left).
  • Time and space complexity: O(n) time, O(1) space with optimized DP.
  • Comparison with greedy approach: greedy may fail due to future dependencies, DP is robust.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.