Lexicographically largest output from a spreading availability mask
I was asked this
No reports yet. Sign in to say where you were asked it.
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 ofnpositive integers; -
state, a string of lengthnmade of'0'and'1'.state[i] = '1'meansarr[i]is available;'0'means it is blocked.
The service runs exactly m operations. In each operation:
- Choose any available index
iand appendarr[i]toS. The element is not consumed; it stays available and may be chosen again later. - Then every blocked index
iwithstate[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-
statecontains 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
mis 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
mand 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
Swith a running maximum: at stept, the best value is the maximum of the previous best and every value that becomes available at stept. Bucketing indices by their unlock step makes it O(n + m). - Edge cases:
m = 0,mlarger thann(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
Sinstead.
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
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
