Modal Lock and Fair Modal Lock
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.
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.
Unlock this recently reported problem
FastPrep Pro gives you full access to interview problems reported within the last week.
- Full problem statement and constraints
- 2 more worked examples, explained
- Guided hints and editorial
- Run your code on real test cases
Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.