Modal Lock and Fair Modal Lock
Updated
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.compatibilityis square, symmetric, and contains only0or1.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 Phone Screen questions
- Secret Number Search with One-Call-Delayed FeedbackPHONE SCREEN · Seen Nov 2026
- Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
- Grid Infection with Recovery After D DaysPHONE SCREEN · Seen Nov 2026
- Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
- Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adaptive Calibration ErrorPHONE SCREEN · Seen Nov 2026
- Elevator Scheduling SimulationPHONE SCREEN · Seen Nov 2026
- Evaluate Noisy AnnotatorsPHONE SCREEN · Seen Oct 2026
- Network Endpoint or Cycle BoundaryOA · PHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Blocking Connection Pool Operation TraceGoogle · ONSITE INTERVIEWGoogle interview questions
- Concurrent Buffered File LoggerDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Dynamic Least-Loaded Job DispatcherCresta · PHONE SCREENCresta interview questions
- Single-Flight Chunk CacheModal · PHONE SCREEN
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- VIP and Regular Service SchedulerIntuit · PHONE SCREENIntuit interview questions
- Timer Job Scheduler Wake-upsRedNote · PHONE SCREEN
- Implement a FIFO QueueByteDance · PHONE SCREENByteDance interview questions
- Stateful Paginated Fetch NLyft · PHONE SCREENLyft interview questions
- Music Player Operation SequenceAtlassian · PHONE SCREENAtlassian interview questions