FastPrepReusable Cyclic Barrier

Reusable Cyclic Barrier

Apple logoApple● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Implement the waiting and reusable-generation behavior of a cyclic barrier through an ordered finite arrival trace. For this exercise, the barrier has a positive number of parties and starts in generation0.

Each entry in participantIds submits one atomic arrival-and-wait call. Once exactly parties calls have arrived in the current generation, release all of them together and advance to the next generation. Before release, a participant cannot submit another call. After release, that participant may immediately arrive in a later generation. Inputs satisfy this rule; a generation need not use the same participants as the previous one.

Return the final result for each arrival after the entire trace: RELEASE g if generation g completed, or WAIT g if its last group is still incomplete. Generation numbers are decimal integers starting at0. No broken-barrier, reset, cancellation or timeout behavior is included.

Function

runCyclicBarrier(parties: int, participantIds: int[]) → String[]

Examples

Example 1

parties = 2participantIds = [1,2,1]return = ["RELEASE 0","RELEASE 0","WAIT 1"]

The first two arrivals release together; the same participant can re-enter and wait in generation1.

Example 2

parties = 3participantIds = [8,9]return = ["WAIT 0","WAIT 0"]

Two arrivals do not complete a three-party generation.

Example 3

parties = 1participantIds = [7,7]return = ["RELEASE 0","RELEASE 1"]

Every single-party generation releases immediately.

Constraints

  • 1 <= parties <= 1000.
  • 0 <= participantIds.length <= 2000.
  • Participant IDs are integers in [0,10^9].
  • No participant repeats within an incomplete generation.

More Apple problems

See Apple hiring insights
public String[] runCyclicBarrier(int parties, int[] participantIds) {
    // Write your code here.
}
parties2
participantIds[1,2,1]
expected["RELEASE 0", "RELEASE 0", "WAIT 1"]
Checking account…