Quick Overview

Given badge records of a name, a minute and an enter or exit action, return two sorted lists: people whose entries and exits do not pair up, and people who badged in three or more times within 60 minutes. Tests grouping, sorting by time, a small state machine and a sliding window check.

Badge access logs: flag mismatched enters and exits and 3 entries within 60 minutes

Company: Pinterest

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list of badge access log records. Each record is `[name, time, action]`, where `name` identifies a person, `time` is a minute of the day written as a string (for example `"100"`), and `action` is `"enter"` or `"exit"`. Find two groups of people: - People with mismatched access: they entered without a corresponding exit, or exited without a corresponding enter. - People who entered a room 3 or more times within any 1-hour window (60 minutes). Return a list of two sorted lists. The first holds the names of people with mismatched access, and the second holds the names of people who entered 3 or more times within a 1-hour window. ### Function Signature ```python def badge_access_logs(records: list[list[str]]) -> list[list[str]]: ``` ### Rules - Records can arrive in any order. Replay each person's records in increasing order of time, comparing times as integers. Records of the same person with the same time keep their input order. - Everyone starts outside. A person has mismatched access if, during that replay, they enter while already inside, exit while already outside, or are still inside after their last record. - Every `"enter"` record counts toward the frequent check, including an entry that is itself a mismatch. A person is frequent when three of their enter times `t1 <= t2 <= t3` satisfy `t3 - t1 <= 60`. The window is inclusive, so entries at minutes 0, 20 and 60 qualify. - Each name appears at most once in each list, and the same person can appear in both. Sort each list in ascending lexicographic order. Either list may be empty. ### Constraints - `1 <= len(records) <= 10^4` - Each record is `[name, time, action]`. - `name` consists of lowercase English letters. - `0 <= int(time) <= 1440` - `action` is either `"enter"` or `"exit"`. ### Examples ```text Input: records = [["alice","100","enter"],["bob","120","enter"],["alice","130","exit"],["bob","140","enter"],["alice","160","enter"],["alice","200","enter"],["bob","300","exit"]] Output: [["alice","bob"],[]] ``` Alice enters at 160 and again at 200 without exiting in between, and she is still inside at the end. Bob enters at 120 and again at 140 without exiting; nobody is frequent, because Alice's three entries (100, 160, 200) span 100 minutes and Bob has only two. ```text Input: records = [["carl","10","enter"],["carl","30","exit"],["carl","50","enter"],["carl","60","enter"],["carl","70","enter"],["dave","5","exit"]] Output: [["carl","dave"],["carl"]] ``` Carl enters at 60 and at 70 while already inside, and his entries at 50, 60 and 70 fall within 20 minutes. Dave exits at 5 without having entered. ```text Input: records = [["erin","60","enter"],["frank","5","enter"],["erin","0","enter"],["erin","70","exit"],["erin","10","exit"],["frank","50","exit"],["erin","20","enter"],["erin","30","exit"]] Output: [[],["erin"]] ``` Sorted by time, Erin alternates enter and exit with no mismatch, and her entries at 0, 20 and 60 span exactly 60 minutes, which counts. Frank enters once and exits once.

Overview: Given badge records of a name, a minute and an enter or exit action, return two sorted lists: people whose entries and exits do not pair up, and people who badged in three or more times within 60 minutes. Tests grouping, sorting by time, a small state machine and a sliding window check.

You are given a list of badge access log records. Each record is `[name, time, action]`, where `name` identifies a person, `time` is a minute of the day written as a string (for example `"100"`), and `action` is `"enter"` or `"exit"`. Find two groups of people: - People with mismatched access: they entered without a corresponding exit, or exited without a corresponding enter. - People who entered 3 or more times within any 1-hour window (60 minutes). Return a list of two sorted lists `[mismatched, frequent]`. The first holds the names of people with mismatched access, and the second holds the names of people who entered 3 or more times within a 1-hour window. ### Rules - Records can arrive in any order. Replay each person's records in increasing order of time, comparing times as integers (so `"9"` comes before `"100"`). Records of the same person with the same time keep their input order. - Everyone starts outside. A person has mismatched access if, during that replay, they enter while already inside, exit while already outside, or are still inside after their last record. - Every `"enter"` record counts toward the frequent check, including an entry that is itself a mismatch. A person is frequent when three of their enter times `t1 <= t2 <= t3` satisfy `t3 - t1 <= 60`. The window is inclusive, so entries at minutes 0, 20 and 60 qualify. - Each name appears at most once in each list, and the same person can appear in both lists. Sort each list in ascending lexicographic order. Either list may be empty. ### Example 1 ```text Input: records = [["alice","100","enter"],["bob","120","enter"],["alice","130","exit"],["bob","140","enter"],["alice","160","enter"],["alice","200","enter"],["bob","300","exit"]] Output: [["alice","bob"],[]] ``` Alice enters at 160 and again at 200 without exiting in between, and she is still inside at the end. Bob enters at 120 and again at 140 without exiting. Nobody is frequent: Alice's three entries (100, 160, 200) span 100 minutes and Bob has only two. ### Example 2 ```text Input: records = [["erin","60","enter"],["frank","5","enter"],["erin","0","enter"],["erin","70","exit"],["erin","10","exit"],["frank","50","exit"],["erin","20","enter"],["erin","30","exit"]] Output: [[],["erin"]] ``` Sorted by time, Erin alternates enter and exit with no mismatch, and her entries at 0, 20 and 60 span exactly 60 minutes, which counts. Frank enters once and exits once. ### Constraints - `1 <= len(records) <= 10^4` - Each record is `[name, time, action]`. - `name` consists of lowercase English letters, and `1 <= len(name) <= 20`. - `time` is a string of decimal digits with no sign and no leading zeros, and `0 <= int(time) <= 1440`. - `action` is either `"enter"` or `"exit"`. - Every time fits in a 32-bit signed integer; no value exceeds 2^31-1.

Constraints

  • 1 <= len(records) <= 10^4
  • Each record is [name, time, action].
  • name consists of lowercase English letters, and 1 <= len(name) <= 20.
  • time is a string of decimal digits with no sign and no leading zeros, and 0 <= int(time) <= 1440.
  • action is either "enter" or "exit".
  • Every time fits in a 32-bit signed integer; no value exceeds 2^31-1.

Examples

Input: ([['amy', '1440', 'enter']],)

Expected Output: [['amy'], []]

Explanation: Single record: a lone enter at the upper time bound 1440 leaves amy inside at the end.

Input: ([['bo', '0', 'exit']],)

Expected Output: [['bo'], []]

Explanation: Single record: a lone exit at time 0 happens while bo is outside.

Hints

  1. Each person's result depends only on that person's own records, so it helps to look at one person at a time.
  2. Order a person's records by time compared as integers ("9" comes before "100"), and keep records with equal times in their input order.
  3. An entry that is itself a mismatch still counts toward the frequent check, and a span of exactly 60 minutes counts.

Audit Badge Logs for Mismatched and Frequent Entries

mediumPinterestSoftware Engineer

You are given a list of badge access log records. Each record is [name, time, action], where name identifies a person, time is a minute of the day written as a string (for example "100"), and action is "enter" or "exit".

Find two groups of people:

  • People with mismatched access: they entered without a corresponding exit, or exited without a corresponding enter.
  • People who entered 3 or more times within any 1-hour window (60 minutes).

Return a list of two sorted lists [mismatched, frequent]. The first holds the names of people with mismatched access, and the second holds the names of people who entered 3 or more times within a 1-hour window.

Rules

  • Records can arrive in any order. Replay each person's records in increasing order of time, comparing times as integers (so "9" comes before "100"). Records of the same person with the same time keep their input order.
  • Everyone starts outside. A person has mismatched access if, during that replay, they enter while already inside, exit while already outside, or are still inside after their last record.
  • Every "enter" record counts toward the frequent check, including an entry that is itself a mismatch. A person is frequent when three of their enter times t1 <= t2 <= t3 satisfy t3 - t1 <= 60. The window is inclusive, so entries at minutes 0, 20 and 60 qualify.
  • Each name appears at most once in each list, and the same person can appear in both lists. Sort each list in ascending lexicographic order. Either list may be empty.

Example 1

Input: records = [["alice","100","enter"],["bob","120","enter"],["alice","130","exit"],["bob","140","enter"],["alice","160","enter"],["alice","200","enter"],["bob","300","exit"]]
Output: [["alice","bob"],[]]

Alice enters at 160 and again at 200 without exiting in between, and she is still inside at the end. Bob enters at 120 and again at 140 without exiting. Nobody is frequent: Alice's three entries (100, 160, 200) span 100 minutes and Bob has only two.

Example 2

Input: records = [["erin","60","enter"],["frank","5","enter"],["erin","0","enter"],["erin","70","exit"],["erin","10","exit"],["frank","50","exit"],["erin","20","enter"],["erin","30","exit"]]
Output: [[],["erin"]]

Sorted by time, Erin alternates enter and exit with no mismatch, and her entries at 0, 20 and 60 span exactly 60 minutes, which counts. Frank enters once and exits once.

Constraints

  • 1 <= len(records) <= 10^4
  • Each record is [name, time, action].
  • name consists of lowercase English letters, and 1 <= len(name) <= 20.
  • time is a string of decimal digits with no sign and no leading zeros, and 0 <= int(time) <= 1440.
  • action is either "enter" or "exit".
  • Every time fits in a 32-bit signed integer; no value exceeds 2^31-1.

Examples

Example 1
Input
([['amy', '1440', 'enter']],)
Output
[['amy'], []]
Notes
Single record: a lone enter at the upper time bound 1440 leaves amy inside at the end.
Example 2
Input
([['bo', '0', 'exit']],)
Output
[['bo'], []]
Notes
Single record: a lone exit at time 0 happens while bo is outside.

Constraints

  • •1 <= len(records) <= 10^4
  • •Each record is [name, time, action].
  • •name consists of lowercase English letters, and 1 <= len(name) <= 20.
  • •time is a string of decimal digits with no sign and no leading zeros, and 0 <= int(time) <= 1440.
  • •action is either "enter" or "exit".
  • •Every time fits in a 32-bit signed integer; no value exceeds 2^31-1.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def badge_access_logs(records: list[list[str]]) -> list[list[str]]:
Sample call
badge_access_logs([['amy', '1440', 'enter']])
Language
Case 1
Input
([['amy', '1440', 'enter']],)
Expected
[['amy'], []]
Case 2
Input
([['bo', '0', 'exit']],)
Expected
[['bo'], []]

Browse More Questions

Show the approach

Approach

Group the records by name and remember each record's input position. For each person, sort their records by integer time, breaking ties by input position. This is the replay order the rules define: records with equal times keep their input order, and "9" sorts before "100". Replay with one flag, inside, that starts false. An enter while inside is true, or an exit while it is false, marks the person as mismatched, and so does inside still being true after the last record. A mark is never cleared, so the state after a bad record cannot change the answer. Every enter time is collected, including enters that are themselves mismatches, and since the replay runs in time order these times come out sorted. If any three enter times t1 <= t2 <= t3 satisfy t3 - t1 <= 60, the three consecutive sorted entries that start at t1 also satisfy it, because the third of them is no later than t3. So checking enters[i] - enters[i - 2] <= 60 for each i finds a qualifying window exactly when one exists. Each name is added at most once per list, and both lists are sorted lexicographically at the end. Edge cases: a single record (a lone enter leaves the person inside, a lone exit happens while outside), equal times for one person, the inclusive 60-minute boundary, a person in both lists, and either or both lists empty.

Time complexity:
O(n log n)
Space complexity:
O(n)