← Questions

Lexicographically largest output from a spreading availability mask

0
AmazonCodingLast asked Apr 2026

A coding interview question asked in Amazon's online assessment, last asked Apr 2026. It tests greedy, simulation and arrays.

A test-data service builds an output array S from two inputs:

  • arr, an array of n positive integers;
  • state, a string of length n made of '0' and '1'. state[i] = '1' means arr[i] is available; '0' means it is blocked.

The service runs exactly m operations. In each operation:

  1. Choose any available index i and append arr[i] to S. The element is not consumed; it stays available and may be chosen again later.
  2. Then every blocked index i with state[i - 1] = '1' becomes available. All of these updates use the state from before this step, so availability moves at most one cell to the right per operation.

Implement:

def generate_array(arr: list[int], state: str, m: int) -> list[int]:
    ...

Return the lexicographically largest S the service can produce.

Constraints

  • 1 <= n <= 10^5, 0 <= m <= 10^5
  • 1 <= arr[i] <= 10^9
  • state contains at least one '1'

Example 1

arr = [3, 8, 2, 9, 4], state = "10000", m = 4
output: [3, 8, 8, 9]

Only index 0 starts available, so the first pick is 3. After that, index 1 unlocks (8), then index 2 (2), then index 3 (9). The best value available at each step is 3, 8, 8, 9.

Example 2

arr = [7, 1, 5, 2], state = "0010", m = 3
output: [5, 5, 5]

Availability only spreads to the right, so 7 and 1 are never reachable. Index 3 unlocks after the first step, but 2 never beats 5.

Clarifying questions

  • If m is 0, should the result be []?
  • Does the spread from one available cell to the next happen even when the cell is never picked?
  • Can availability ever be lost, or does it only grow?
  • Is the output compared lexicographically element by element, with all outputs having length exactly m?

What a strong answer covers

  • Sees that the choice of what to append never changes availability, so every output has length m and the greedy pick of the largest available value at each step is optimal.
  • Computes for each index the step when it becomes available: its distance to the nearest '1' at or before it, or never if there is none to its left.
  • Builds S with a running maximum: at step t, the best value is the maximum of the previous best and every value that becomes available at step t. Bucketing indices by their unlock step makes it O(n + m).
  • Edge cases: m = 0, m larger than n (later steps repeat the overall maximum of the reachable part), blocked cells to the left of every '1', and every cell already available.
  • Tests: both examples, an all-'1' state, a single '1' at the last index, and a brute-force simulation on small random inputs.

Follow-ups

  • Picking an element consumes it, so it can't be chosen again.
  • Availability spreads in both directions.
  • Each pick must be at an index at least as large as the previous pick.
  • Return the lexicographically smallest S instead.

Where it's asked

  • Amazon · Online assessment · Last asked Apr 2026Coding problems plus a repository bug-fix section with a built-in assistant or a design scenario, then a work-style survey · AI only inside the bug-fix section's built-in assistant

What it tests

greedy · simulation · arrays

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding