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
Security Rule: An account should be banned if it was logged in from two different IP addresses at any overlapping time period.
Table Schema:
+-------------+----------+
| Column Name | Type |
+-------------+----------+
| account_id | int |
| ip_address | int |
| login | datetime |
| logout | datetime |
+-------------+----------+
Return a list of
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
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)
โก Linearithmic
Space Complexity
O(n)
Space for events array and active sessions tracking
โก 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
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code