14 September 2026 · Advanced track
Crate Inspection Order
Find the first warehouse inspection event that breaks the required crate-closing order.
A warehouse logs crate inspections in order. Every event contains an action, either open or close, and a crate identifier. For each event: - An open event begins inspecting that crate. - A close event must close the most recently opened crate that has not yet been closed, and it must use the same identifier. Return the zero-based index of the first event that breaks these rules. If every event follows the rules but some inspections are still open at the end, return the number of events. Return -1 when the whole log is valid.
Constraints
events contains from 0 to 1,000 recordsEach action is exactly open or closeEach crate identifier contains from 1 to 30 lowercase ASCII letters
Function signature
def first_inspection_error(events):