Problem · Greedy

Create Array Generator Service

MediumAmazonNEW GRADFULLTIMEOA
See Amazon hiring insights

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:

  • arr[n] contains n positive integers.
  • state is a string that contains n characters. Each character is a '0' or '1'. If state[i] = '1', arr[i] is available. If state[i] = '0', arr[i] is blocked.

To create an integer array, S, the following operation is performed exactly m times. S is initially empty.

  • Choose any arr[i] that is available, that is, where state[i] = '1'. The same element can be chosen any number of times.
  • Append the value in arr[i] to S.
  • For all state[i] = '0' such that state[i-1] = '1', change state[i] to '1'. For example, if state = '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 such i (0 ≤ i < m), that x[i] > y[i], and for any j (0 ≤ j < i) x[j] = y[j].

Examples
01 · Example 1
elements = [10, 5, 7, 6]
availability = "0101"
operations = 2
return = [6, 7]
This test case was added on March 1, 2025, for your testing convenience. Relevant information is included in the source image section below.

Step 1: Initially, only {6} is available since availability[3] = '1'.

Step 2: Select 6. This unlocks availability[2], making {7} available.

Step 3: Select 7. We have now performed operations = 2 selections.

02 · Example 2
elements = [4, 9, 1, 2, 10]
availability = "10010"
operations = 4
return = [4, 9, 10, 10]
This test case was added on March 1, 2025, for your testing convenience. Relevant information is included in the source image section below.

Step 1: Initially available elements: {4, 10}.

Step 2: Select 4, unlocking availability[1] → {4} → {9}.

Step 3: Select 9, unlocking availability[2] → {9} → {1}.

Step 4: Select 10, unlocking availability[3] → {10} → {2}.

Step 5: Select 10 again.

03 · Example 3
arr = [5, 3, 4, 6]
state = "1100"
m = 5
return = [5, 5, 6, 6, 6]
Initially available elements are arr[0] = 5 and arr[1] = 3 (state = '1100'). Following the rule of selecting the largest available element (preferring the highest index on ties) and unlocking adjacent locked elements after each selection for m = 5 operations yields [5, 5, 6, 6, 6].
Constraints
  • arr has length n and contains positive integers.
  • state is a string of n characters, each being '0' or '1'.
  • The selection operation is performed exactly m times.
More Amazon problems
drafts saved locally
public int[] generateOptimalSequence(int[] elements, String availability, int operations) {
  // write your code here
}
elements[10, 5, 7, 6]
availability"0101"
operations2
expected[6, 7]
sign in to submit