FastPrepWho Is in the Room?

Who Is in the Room?

WhatNot logoWhatNot● MediumINTERNPHONE SCREEN
Learn

Problem statement

Given an ordered event log, determine which observed people are inside a room after all events.

For this exercise, each event is [name, action], where action is enter or exit. Everyone starts outside. An entry sets that person's state to IN; an exit sets it to OUT. Repeating the same action is allowed and does not change its meaning.

Return one row [name, state] for every person appearing in the log, including people who finish outside. Sort rows lexicographically by name. The answer for an empty log is an empty array.

Function

roomPresence(events: String[][]) → String[][]

Examples

Example 1

events = [["amy","enter"],["bob","enter"],["amy","exit"]]return = [["amy","OUT"],["bob","IN"]]

Amy's latest event is an exit; Bob's is an entry. Rows are ordered by name.

Example 2

events = [["zoe","exit"],["zoe","enter"],["zoe","enter"]]return = [["zoe","IN"]]

An entry sets the state to IN. Repeated entries keep the same state.

Example 3

events = []return = []

No observed people means no output rows.

Constraints

  • For this exercise, assume 0 <= events.length <= 200.
  • Every event has exactly two strings.
  • Names contain 1 to 20 lowercase English letters.
  • Actions are exactly enter or exit.

More WhatNot problems

See WhatNot hiring insights
public String[][] roomPresence(String[][] events) {
    // write your code here
}
events[["amy","enter"],["bob","enter"],["amy","exit"]]
expected[["amy", "OUT"], ["bob", "IN"]]
Checking account…