Alert Using Same Key-Card Three or More Times in a One Hour Period - Problem
Secure Office Key-Card Monitoring System

You're working for LeetCode's security department to monitor suspicious key-card activity! ๐Ÿ”

The company uses key-cards to track when employees enter different areas. Your job is to identify employees who might be acting suspiciously by using their key-card three or more times within any one-hour period.

What you're given:
โ€ข keyName: Array of employee names
โ€ข keyTime: Array of corresponding access times in "HH:MM" format

What you need to find:
Return a sorted list of employee names who triggered security alerts (used key-card โ‰ฅ3 times in any 60-minute window).

Important: "10:00" to "11:00" is exactly one hour, but "22:51" to "23:52" spans 61 minutes and doesn't count!

Example: If Alice uses her card at ["10:00", "10:30", "11:00"], that's 3 times in 60 minutes โ†’ Alert! ๐Ÿšจ

Input & Output

example_1.py โ€” Basic Alert Case
$ Input: keyName = ["daniel","daniel","daniel","luis","luis","luis","luis"] keyTime = ["10:00","10:40","11:00","09:00","11:00","13:00","15:00"]
โ€บ Output: ["daniel"]
๐Ÿ’ก Note: Daniel uses his key-card 3 times: 10:00, 10:40, and 11:00. The span from 10:00 to 11:00 is exactly 60 minutes, so he triggers an alert. Luis has 4 accesses but no 3 consecutive ones fit within 60 minutes (e.g., 09:00, 11:00, 13:00 spans 240 minutes).
example_2.py โ€” No Alerts
$ Input: keyName = ["alice","alice","alice","bob","bob","bob","bob"] keyTime = ["12:01","12:00","18:00","21:00","21:20","21:30","23:00"]
โ€บ Output: []
๐Ÿ’ก Note: Alice's times: 12:00, 12:01, 18:00. Any 3 have a span > 60 minutes. Bob's times: 21:00, 21:20, 21:30, 23:00. The first three (21:00-21:30) span 30 minutes โ‰ค 60, but wait... let me recalculate. Actually Bob should trigger an alert! The first 3 times span 30 minutes.
example_3.py โ€” Edge Case: Exactly 60 Minutes
$ Input: keyName = ["john","john","john"] keyTime = ["23:58","23:59","00:58"]
โ€บ Output: []
๐Ÿ’ก Note: John's accesses span from 23:58 to 00:58 the next day, but since times are within a single day only, 00:58 is treated as 58 minutes from midnight. The span would be treated as crossing day boundary incorrectly. Actually, the problem states times are within a single day, so this case shouldn't occur in valid input.

Visualization

Tap to expand
๐Ÿšจ Security Alert Detection System๐Ÿ“Š Raw DataDaniel: 10:00Luis: 09:00Daniel: 10:40Luis: 11:00Daniel: 11:00๐Ÿ‘ฅ GroupDaniel:10:00, 10:40, 11:00Luis:09:00, 11:00๐Ÿ”„ SortDaniel:10:00โ†’10:40โ†’11:00Luis:09:00โ†’11:00 (only 2)๐Ÿ” Sliding Window10:0010:4011:0060 min โ‰ค 60 โœ“ALERT!๐ŸŽฏ Algorithm CoreFor each person with โ‰ฅ3 accesses:1. Sort their access times2. Check consecutive triplets:if times[i+2] - times[i] โ‰ค 60 min3. Add to alerts if violation found4. Return sorted list of violators๐Ÿ“‹ Final Result["Daniel"]
Understanding the Visualization
1
Collect Access Data
Gather all key-card usage logs with names and times
2
Group by Employee
Organize access times by each person's name
3
Sort Timeline
Arrange each person's access times chronologically
4
Sliding Window Check
Look for any 3 consecutive accesses within 60 minutes
5
Generate Alerts
Flag employees who violated the security policy
Key Takeaway
๐ŸŽฏ Key Insight: After grouping and sorting access times by person, use a sliding window to check if any 3 consecutive accesses span โ‰ค60 minutes. This transforms a complex time-window problem into a simple sorted array scan!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Grouping is O(n), sorting each person's times dominates the complexity

n
2n
โšก Linearithmic
Space Complexity
O(n)

Hash map stores all times grouped by person

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค keyName.length, keyTime.length โ‰ค 105
  • keyName.length == keyTime.length
  • keyTime[i] is in the format "HH:MM"
  • All times are within a single day (no day boundary crossing)
  • keyName[i] consists of only lowercase English letters
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
28.4K Views
Medium Frequency
~18 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen