Ordered Badge Entry and Exit Violations
Problem statement
Each row of records is [name,action], where action is enter or exit. Process the records in their given order and identify people whose badge history has entry or exit violations.
A valid history alternates enter,exit,enter,exit,.... An entry while already inside means an exit is missing. An exit while outside means an entry is missing.
For this exercise, assume everyone starts outside. After any enter, the person is inside; after any exit, the person is outside, including after an invalid repeated action. A person still inside at the end has an entry without an exit. Later valid events never erase a previously recorded violation.
Return exactly two rows: the first contains names with an entry without an exit, and the second contains names with an exit without an entry. For this exercise, assume each row contains unique names sorted lexicographically, names are case-sensitive, and an empty history returns [[],[]].
For this exercise, assume the two reported categories refer to violations of this ordered alternating flow, including repeated actions and a final open entry.
Function
badgeViolations(records: String[][]) → String[][]Examples
Example 1
records = [["Sarah","enter"],["john","exit"],["Sarah","enter"],["Mike","enter"],["Abdul","enter"],["Sarah","exit"]]return = [["Abdul","Mike","Sarah"],["john"]]Using the stated practice rules, Sarah's repeated entry is retained as a violation even after her exit. Mike and Abdul end inside; john exits while outside. The original report gives this input without an expected output.
Example 2
records = [["u","enter"],["u","enter"],["u","exit"],["u","exit"]]return = [["u"],["u"]]The practice flow detects both repeated actions even though the total entry and exit counts are equal.
Constraints
- For this exercise, assume
0 <= records.length <= 1000. - Each row has exactly two strings; the action is
enterorexit. - Names are case-sensitive alphanumeric strings with lengths from
1through30.