Skip to content
← Back to the archive

14 September 2026 · Advanced track

Crate Inspection Order

Advanced Lists Records Stacks

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 records
  • Each action is exactly open or close
  • Each crate identifier contains from 1 to 30 lowercase ASCII letters

Function signature

def first_inspection_error(events):

Sign in to solve this bracket

Reading the problem is free for everyone. Opening the editor and solving it requires an account with Open Bracket+.