Create Array Generator Service
Learn this problemProblem statement
Your project team needs to work closely with a group of software testers. They have requested that your team create an array generator service to assist with testing software functionality. Create an array generator service.
Its input parameters are:
values[n]containsnpositive integers.stateis a string that containsncharacters.- Each character is either
'0'or'1'. - If
state[i] = '1',values[i]is available. - If
state[i] = '0',values[i]is blocked.
- Each character is either
To create an integer array, S, the following operation is performed exactly m times. S is initially empty.
- Choose any
values[i]that is available, that is, wherestate[i] = '1'. The same element can be chosen any number of times. - Append the value in
values[i]toS. - For all
state[i] = '0'such thati > 0andstate[i-1] = '1'before this unlock step, changestate[i]to'1'. For example, ifstate = '0100101'before the operation, it equals'0110111'after the operation.
Find the lexicographically largest sequence S that can be obtained after m operations.
Note: A sequence x of length m is lexicographically larger than sequence y of length m if there exists an index i (0 ≤ i < m) such that x[i] > y[i], and for every j (0 ≤ j < i), x[j] = y[j].
Function
getOptimalSequence(values: int[], state: String, m: int) → int[]Examples
Example 1
values = [10,5,7,6]state = "0101"m = 2return = [6,7]010101110111state = "0101", so indices1and3are available. Amongvalues[1] = 5andvalues[3] = 6, choose6and append it toS.- The unlock step changes
statefrom"0101"to"0111", making index2available. - The highest available value is now
values[2] = 7. After exactlym = 2operations, return[6, 7].
Thank you to a kind friend for helping correct the mistakes in this example! 🌷(06-23-2026 :D
Example 2
values = [4,9,1,2,10]state = "10010"m = 4return = [4,10,10,10]10010110111111111111state = "10010", so indices0and3are available. The available values are4and2, so choose4.- The unlock step uses the state before unlocking, so indices
1and4become available andstatebecomes"11011". Now the largest available value is10. - The next unlock step makes index
2available, but10remains the largest available value. Choose10for the remaining operations and return[4, 10, 10, 10].
Thank you to a kind friend for helping correct the mistakes in this example! 🌷(06-23-2026 :D
Example 3
values = [5,3,4,6]state = "1100"m = 5return = [5,5,6,6,6]1100111011111111state = "1100", so indices0and1are initially available. The largest available value isvalues[0] = 5.- After the first unlock step, index
2becomes available andstatebecomes"1110". The largest available value is still5, so choose it again. - After the second unlock step, index
3becomes available andstatebecomes"1111". The largest available value is6, so choose6for the remaining three operations.
Constraints
valueshas lengthnand contains positive integers.statehas lengthnand contains only'0'and'1'.- The operation is performed exactly
mtimes.
More Amazon problems
- HTTP Request RedirectionOA · Seen Jul 2026
- Merge IntervalsOA · Seen Jul 2026
- Permutation SorterOA · Seen Jul 2026
- Resolve Task DependenciesONSITE INTERVIEW · Seen Jul 2026
- Shortest Distance on a Circular Bus RouteOA · Seen Jul 2026
- Longest Increasing Subsequence With Bounded Adjacent DifferenceONSITE INTERVIEW · Seen Jul 2026
- Search in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Jul 2026
- Sliding Window MaximumONSITE INTERVIEW · Seen Jul 2026