Problem · Design

Modal Lock and Fair Modal Lock

HardOpenAI logoOpenAIFULLTIMEPHONE SCREEN

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.

The problem statement continues
Pro

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.

FastPrep Pro
Reported in 1 OpenAI interview this week

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
$9/month

Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week
CodePython 3
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 OpenAI interview this week

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
$9/month

Pro subscription, billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week