FastPrepModal Lock and Fair Modal Lock

Modal Lock and Fair Modal Lock

OpenAI logoOpenAI● HardFULLTIMEPHONE SCREEN
Learn

Problem statement

Simulate two independent modal locks. Lock type 0 is a non-fair modal lock, and lock type 1 is a fair modal lock. A symmetric matrix compatibility describes which requested modes may be held together: mode a is compatible with held mode b when compatibility[a][b] == 1.

Each operation is [lockType, action, threadId, mode], where action == 0 requests acquisition and action == 1 releases a held mode. The two locks have separate holders and FIFO wait queues.

  • An acquisition is compatible only when its requested mode is compatible with every currently held mode.
  • For the non-fair lock, a compatible new request is granted immediately even when older requests are queued. Otherwise it joins the queue.
  • For the fair lock, a new request is granted immediately only when it is compatible and the queue is empty. Otherwise it joins the queue.
  • After a release, inspect the queue from the front. Grant and remove compatible requests in FIFO order, updating the holder set after each grant. Stop at the first request that is not compatible.

Return one row per operation. The row contains, in grant order, every thread whose acquisition became granted because of that operation. A queued acquisition or a release that grants nobody contributes an empty row.

Function

simulateModalLocks(compatibility: int[][], operations: int[][]) → int[][]

Examples

Example 1

compatibility = [[1,1],[1,0]]operations = [[0,0,1,1],[0,0,2,0],[0,0,3,1],[0,1,1,1]]return = [[1],[2],[],[3]]

Threads 1 and 2 hold compatible modes. Thread 3 waits because mode 1 conflicts with the same held mode, then is granted when thread 1 releases.

Example 2

compatibility = [[1,1],[1,0]]operations = [[1,0,1,1],[1,0,2,1],[1,0,3,0],[1,1,1,1]]return = [[1],[],[],[2,3]]

On the fair lock, thread 3 queues behind thread 2 even though its mode is compatible with the current holder. The release grants the compatible FIFO prefix 2, 3.

Example 3

compatibility = [[1,1],[1,0]]operations = [[0,0,1,1],[0,0,2,1],[0,0,3,0],[0,1,1,1]]return = [[1],[],[3],[2]]

The non-fair lock lets compatible thread 3 barge ahead of queued thread 2. After thread 1 releases, thread 2 becomes compatible with the remaining holder and is granted.

Constraints

  • 1 <= compatibility.length <= 20.
  • compatibility is square, symmetric, and contains only 0 or 1.
  • 1 <= operations.length <= 2000.
  • Every operation has four integers and uses a valid lock type, action, and mode.
  • Within each lock, a thread has at most one held or pending request at a time.
  • Every release names a thread that currently holds the stated mode on that lock.

More OpenAI problems

See OpenAI hiring insights
public int[][] simulateModalLocks(int[][] compatibility, int[][] operations) {
    // Write your code here.
}
compatibility[[1,1],[1,0]]
operations[[0,0,1,1],[0,0,2,0],[0,0,3,1],[0,1,1,1]]
expected[[1],[2],[],[3]]
Checking account…