FastPrepFIFO Mutex Operation Trace

FIFO Mutex Operation Trace

Apple logoApple● MediumFULLTIMEONSITE INTERVIEW
Learn

Problem statement

Implement a non-reentrant mutex without using a prebuilt lock. For this exercise, use a finite trace of atomic ACQUIRE and RELEASE calls, paired with integer actor IDs. This trace adapter models ownership, waiting and handoff.

The lock initially has no owner. An acquire on a free lock completes immediately and makes its actor the sole owner. Other acquires wait in FIFO submission order. When the owner releases, its call completes, then the oldest waiting acquire becomes the owner and completes immediately. If nobody waits, the lock becomes free.

Inputs are valid: an actor acquires only when it neither owns nor waits for the lock, and releases only while it is the current owner. After releasing, it may acquire again. Return the final result per call after the entire trace: ACQUIRED for an acquisition eventually granted, WAIT for an acquisition still waiting, and RELEASED for a release. No reentrancy, try-lock, cancellation or timeout operation is included.

Function

runFifoMutex(operations: String[], actors: int[]) → String[]

Examples

Example 1

operations = ["ACQUIRE","ACQUIRE","RELEASE"]actors = [1,2,1]return = ["ACQUIRED","ACQUIRED","RELEASED"]

Actor2 first waits, then receives ownership when actor1 releases.

Example 2

operations = ["ACQUIRE","ACQUIRE"]actors = [1,2]return = ["ACQUIRED","WAIT"]

No release grants actor2 before trace end.

Example 3

operations = ["ACQUIRE","ACQUIRE","ACQUIRE","RELEASE","RELEASE"]actors = [1,2,3,1,2]return = ["ACQUIRED","ACQUIRED","ACQUIRED","RELEASED","RELEASED"]

Each release transfers ownership to the oldest waiting actor.

Constraints

  • 0 <= operations.length = actors.length <= 2000.
  • Each operation is ACQUIRE or RELEASE.
  • Actor IDs are integers in [0,10^9].
  • Every trace satisfies the ownership and pending-acquire validity rules.

More Apple problems

See Apple hiring insights
public String[] runFifoMutex(String[] operations, int[] actors) {
    // Write your code here.
}
operations["ACQUIRE","ACQUIRE","RELEASE"]
actors[1,2,1]
expected["ACQUIRED", "ACQUIRED", "RELEASED"]
Checking account…