Find the first invalid event in a LIFO lock acquire and release log

Quick Overview

Given a single-threaded log of lock acquire and release events that must follow LIFO order, return the position of the first invalid event, or a code for a clean or unfinished log. It tests stack simulation, constant-time membership checks and careful handling of the return conditions.

Find the first invalid event in a LIFO lock acquire and release log

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an array of strings `events` representing a single-threaded log of lock operations. Each event is either `ACQUIRE id` or `RELEASE id`, where `id` is a positive integer. Acquired locks must be released in LIFO order. An event is invalid if it: - re-acquires an ID that is currently held, - releases an ID that is not currently held, or - releases any ID other than the most recently acquired lock that is still held. After a valid release, an ID may be acquired again. Return the 1-based position of the first invalid event. If all `N` events are valid but some locks are still held at the end, return `N + 1`. Otherwise, return `0`. An empty log is valid. ### Function Signature ```python def check_log_history(events: list[str]) -> int: ``` ### Rules - Positions are 1-based: the first event is position 1. - Only the first invalid event matters. Once an event is invalid, the events after it and any locks still held do not change the answer. - `N + 1` is returned only when every event is valid and at least one lock is still held after the last event. ### Constraints - `0 <= len(events) <= 10^5` - Each `events[i]` is well-formed: it is either `ACQUIRE id` or `RELEASE id`, with the operation and the ID separated by a single space. - `1 <= id <= 10^9` ### Examples ```text Input: events = ["ACQUIRE 364", "ACQUIRE 84", "RELEASE 84", "RELEASE 364"] Output: 0 ``` Lock 84 is acquired after 364 and released first, so the releases follow LIFO order. No locks remain held, so the answer is 0. ```text Input: events = ["ACQUIRE 364", "ACQUIRE 84", "RELEASE 364", "RELEASE 84"] Output: 3 ``` At event 3, the most recently acquired held lock is 84, but the event releases 364. That is a crossed release, so the answer is 3. ```text Input: events = ["ACQUIRE 5", "RELEASE 5", "ACQUIRE 5", "ACQUIRE 7", "RELEASE 7"] Output: 6 ``` Lock 5 is released at event 2, so acquiring it again at event 3 is valid. Every event is valid, but lock 5 is still held after the last event, so the answer is N + 1 = 6.

Overview: Given a single-threaded log of lock acquire and release events that must follow LIFO order, return the position of the first invalid event, or a code for a clean or unfinished log. It tests stack simulation, constant-time membership checks and careful handling of the return conditions.

General logo
General
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given an array of strings events representing a single-threaded log of lock operations. Each event is either ACQUIRE id or RELEASE id, where id is a positive integer.

Acquired locks must be released in LIFO order. An event is invalid if it:

  • re-acquires an ID that is currently held,
  • releases an ID that is not currently held, or
  • releases any ID other than the most recently acquired lock that is still held.

After a valid release, an ID may be acquired again.

Return the 1-based position of the first invalid event. If all N events are valid but some locks are still held at the end, return N + 1. Otherwise, return 0. An empty log is valid.

Function Signature

def check_log_history(events: list[str]) -> int:

Rules

  • Positions are 1-based: the first event is position 1.
  • Only the first invalid event matters. Once an event is invalid, the events after it and any locks still held do not change the answer.
  • N + 1 is returned only when every event is valid and at least one lock is still held after the last event.

Constraints

  • 0 <= len(events) <= 10^5
  • Each events[i] is well-formed: it is either ACQUIRE id or RELEASE id , with the operation and the ID separated by a single space.
  • 1 <= id <= 10^9

Examples

Input: events = ["ACQUIRE 364", "ACQUIRE 84", "RELEASE 84", "RELEASE 364"]
Output: 0

Lock 84 is acquired after 364 and released first, so the releases follow LIFO order. No locks remain held, so the answer is 0.

Input: events = ["ACQUIRE 364", "ACQUIRE 84", "RELEASE 364", "RELEASE 84"]
Output: 3

At event 3, the most recently acquired held lock is 84, but the event releases 364. That is a crossed release, so the answer is 3.

Input: events = ["ACQUIRE 5", "RELEASE 5", "ACQUIRE 5", "ACQUIRE 7", "RELEASE 7"]
Output: 6

Lock 5 is released at event 2, so acquiring it again at event 3 is valid. Every event is valid, but lock 5 is still held after the last event, so the answer is N + 1 = 6.