Karat / VO Coding Interview Problem: Badge Access Log Missing Entry or Exit Records

17 Views
No Comments

We are working on a security system for a badged-access room in our company’s building.

Given an ordered list of employees who used their badge to enter or exit the room, write a function that returns two collections:

  • All employees who didn’t use their badge while exiting the room — they recorded an enter without a matching exit. (All employees are required to leave the room before the log ends.)
  • All employees who didn’t use their badge while entering the room — they recorded an exit without a matching enter. (The room is empty when the log begins.)

Each collection should contain no duplicates, regardless of how many times a given employee matches the criteria for belonging to it.

For example:

records = [["Paul", "enter"],
  ["Pauline", "exit"],
  ["Paul", "enter"],
  ["Paul", "exit"],
  ["Martha", "exit"],
  ["Joe", "enter"],
  ["Martha", "enter"],
  ["Steve", "enter"],
  ["Martha", "exit"],
  ["Jennifer", "enter"],
  ["Joe", "enter"],
  ["Curtis", "exit"],
  ["Curtis", "enter"],
  ["Joe", "exit"],
  ["Martha", "enter"],
  ["Martha", "exit"],
  ["Jennifer", "exit"],
  ["Joe", "enter"],
  ["Joe", "enter"],
  ["Martha", "exit"],
  ["Joe", "exit"]
]

This problem asks you to scan an ordered badge log and identify two groups of employees: those who entered without a matching exit, and those who exited without a matching entry. A clean solution uses a set or map to track each employee's current room state while iterating through the records once. Whenever the observed action is inconsistent with the tracked state, add that employee to the appropriate result set to avoid duplicates. The key challenges are handling repeated entries for the same person, preserving order-independent deduped results, and respecting the initial empty-room condition.

END
 0