Account
Draft saved on this device
▤ DescriptionOriginal CppValley problem
ConcurrencyEasy

Validate a Mutex Ownership Trace

Find invalid lock and unlock operations in a sequentially recorded trace.

A debugger records mutex operations in a single chronological trace. Each event is either L thread_id (lock) or U thread_id (unlock). The mutex is initially unlocked. A lock is valid only when it is unlocked; an unlock is valid only when the requesting thread owns it. Invalid operations increment an error counter and leave mutex state unchanged. Count errors. This is a trace-validation exercise, not a substitute for true concurrent race detection.

Input format

First line: N. Next N lines: L id or U id.

Output format

Print the number of invalid operations.

Examples

Example 1
Input
6
L 1
L 2
U 2
U 1
L 2
U 2
Expected output
2

Thread 2 cannot lock or unlock the mutex while thread 1 owns it.

Constraints

  • 0 ≤ N ≤ 100000
  • 1 ≤ id ≤ 10^9

Related concepts

MutexState MachinesDebugging

This early-access exercise uses standard input/output. More advanced concurrency challenges will use specialized class interfaces, instrumentation, and stress tests once the judging infrastructure is ready.

⌘ CodeC++17
GNU C++17 · main.cpp

Loading Monaco C++ editor…

Checking judge...

Checking whether the external C++ judge is available.

⌁ Test cases & resultsPublic samples: 1
Input
6
L 1
L 2
U 2
U 1
L 2
U 2
Expected output
2
Execution requires a secure external judge.