Leetflex Banned Accounts - Problem
Leetflex Account Security Monitor

You're working as a security engineer at Leetflex, a popular coding platform. Your task is to identify suspicious account activity by detecting accounts that are logged in from multiple IP addresses simultaneously.

Given a LogInfo table containing login/logout records with timestamps and IP addresses, you need to find accounts that should be banned for security violations.

Security Rule: An account should be banned if it was logged in from two different IP addresses at any overlapping time period.

Table Schema:
LogInfo
+-------------+----------+
| Column Name | Type |
+-------------+----------+
| account_id | int |
| ip_address | int |
| login | datetime |
| logout | datetime |
+-------------+----------+

Return a list of account_id values that should be banned, in any order.

Input & Output

example_1.py โ€” Basic Overlap Case
$ Input: LogInfo = [\n [1001, 192168001001, '2023-01-01 10:00:00', '2023-01-01 12:00:00'],\n [1001, 100000001, '2023-01-01 11:00:00', '2023-01-01 13:00:00'],\n [1002, 192168001001, '2023-01-01 14:00:00', '2023-01-01 16:00:00']\n]
โ€บ Output: [1001]
๐Ÿ’ก Note: Account 1001 was logged in from IP 192.168.1.1 (10:00-12:00) and IP 10.0.0.1 (11:00-13:00) with overlapping time from 11:00-12:00. Account 1002 only has one session, so it's not banned.
example_2.py โ€” No Overlapping Sessions
$ Input: LogInfo = [\n [2001, 192168001001, '2023-01-01 09:00:00', '2023-01-01 10:00:00'],\n [2001, 100000001, '2023-01-01 11:00:00', '2023-01-01 12:00:00'],\n [2002, 192168001002, '2023-01-01 13:00:00', '2023-01-01 14:00:00']\n]
โ€บ Output: []
๐Ÿ’ก Note: Account 2001 has sessions from different IPs but they don't overlap in time (10:00 end vs 11:00 start). Account 2002 has only one session. No accounts should be banned.
example_3.py โ€” Multiple Violations
$ Input: LogInfo = [\n [3001, 192168001001, '2023-01-01 08:00:00', '2023-01-01 12:00:00'],\n [3001, 100000001, '2023-01-01 10:00:00', '2023-01-01 11:00:00'],\n [3001, 172016001, '2023-01-01 10:30:00', '2023-01-01 11:30:00']\n]
โ€บ Output: [3001]
๐Ÿ’ก Note: Account 3001 has three overlapping sessions from different IP addresses. Even with multiple violations, the account appears only once in the result.

Visualization

Tap to expand
Account 1001 - Session AnalysisChecking for overlapping sessions from different IPs10:0011:0012:0013:0014:00IP: 192.168.1.1Session 1:IP: 10.0.0.1Session 2:OVERLAP11:00 - 12:00โš ๏ธ SECURITY VIOLATION DETECTEDAccount 1001 logged in from 2 different IPs simultaneouslyโ†’ ACCOUNT BANNED
Understanding the Visualization
1
Identify Sessions
Each row represents a login session with start/end times and IP address
2
Compare Pairs
For each account, compare every pair of sessions
3
Check Overlap
Two intervals overlap if start1 < end2 AND start2 < end1
4
Verify Different IPs
Overlapping sessions must be from different IP addresses
5
Flag Account
Mark account as banned if violation is found
Key Takeaway
๐ŸŽฏ Key Insight: Two time intervals [a,b] and [c,d] overlap if and only if a < d AND c < b. This mathematical property allows us to efficiently detect suspicious login patterns in SQL using a simple join condition.

Time & Space Complexity

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

Sorting events takes O(n log n), processing events takes O(n)

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

Space for events array and active sessions tracking

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค number of records โ‰ค 104
  • 1 โ‰ค account_id โ‰ค 104
  • 1 โ‰ค ip_address โ‰ค 231 - 1
  • logout time is always after login time
  • The table may contain duplicate rows
Asked in
Meta 45 Amazon 38 Google 32 Microsoft 28
52.3K Views
Medium Frequency
~15 min Avg. Time
1.8K 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