FastPrepOrdered Badge Entry and Exit Violations

Ordered Badge Entry and Exit Violations

Atlassian logoAtlassian● EasyFULLTIMEPHONE SCREEN
Learn

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 [[],[]].

Function

badgeViolations(records: String[][]) → String[][]

Examples

Example 1

records = [["Arjun","enter"],["Riya","exit"],["Arjun","enter"],["Arjun","exit"],["Meera","exit"],["Kabir","enter"],["Meera","enter"],["Amit","enter"],["Meera","exit"],["Sneha","enter"],["Kabir","enter"],["Rahul","exit"],["Rahul","enter"],["Kabir","exit"],["Meera","enter"],["Meera","exit"],["Sneha","exit"],["Kabir","enter"],["Kabir","enter"],["Meera","exit"],["Kabir","exit"],["Kabir","exit"]]return = [["Amit","Arjun","Kabir","Rahul"],["Kabir","Meera","Rahul","Riya"]]

The original report's violation sets are retained and sorted. Arjun has a repeated entry; Amit and Rahul end inside. Meera, Riya, Rahul and Kabir have exits without an intervening entry.

Example 2

records = [["Arjun","enter"],["Arjun","exit"]]return = [[],[]]

This original sample follows the required alternating flow and produces two empty lists.

Example 3

records = [["Arjun","enter"],["Arjun","enter"],["Arjun","exit"],["Arjun","exit"]]return = [["Arjun"],["Arjun"]]

This original sample has balanced total counts, but both the repeated entry and repeated exit are violations.

Constraints

  • For this exercise, assume 0 <= records.length <= 1000.
  • Each row has exactly two strings; the action is enter or exit.
  • Names are case-sensitive alphanumeric strings with lengths from 1 through 30.

More Atlassian problems

See Atlassian hiring insights
public String[][] badgeViolations(String[][] records) {
    // Write your code here.
    return new String[0][0];
}
records[["Arjun","enter"],["Riya","exit"],["Arjun","enter"],["Arjun","exit"],["Meera","exit"],["Kabir","enter"],["Meera","enter"],["Amit","enter"],["Meera","exit"],["Sneha","enter"],["Kabir","enter"],["Rahul","exit"],["Rahul","enter"],["Kabir","exit"],["Meera","enter"],["Meera","exit"],["Sneha","exit"],["Kabir","enter"],["Kabir","enter"],["Meera","exit"],["Kabir","exit"],["Kabir","exit"]]
expected[["Amit", "Arjun", "Kabir", "Rahul"], ["Kabir", "Meera", "Rahul", "Riya"]]
Checking account…