Leetflex Banned Accounts - Problem

You are given a table LogInfo that contains information about login and logout activities for Leetflex accounts. Each row represents a login session with the account ID, IP address used, and the login/logout timestamps.

Problem: Find all account IDs that should be banned from Leetflex. An account should be banned if it was logged in at some moment from two different IP addresses simultaneously (i.e., there was an overlap in login sessions from different IPs).

Key insight: You need to detect overlapping time periods for the same account but different IP addresses.

Table Schema

LogInfo
Column Name Type Description
account_id PK int Account identifier
ip_address PK int IP address used for login
login PK datetime Login timestamp
logout datetime Logout timestamp
Primary Key: account_id, ip_address, login
Note: Table may contain duplicate rows. Logout time is guaranteed to be after login time.

Input & Output

Example 1 — Overlapping Sessions
Input Table:
account_id ip_address login logout
1 1 2021-02-01 09:00:00 2021-02-01 11:00:00
1 2 2021-02-01 10:00:00 2021-02-01 12:00:00
2 6 2021-02-01 13:00:00 2021-02-01 15:00:00
2 7 2021-02-01 16:00:00 2021-02-01 18:00:00
Output:
account_id
1
💡 Note:

Account 1 should be banned because it was logged in simultaneously from two different IP addresses (1 and 2). The first session (IP 1) was from 09:00 to 11:00, and the second session (IP 2) was from 10:00 to 12:00. These sessions overlap from 10:00 to 11:00.

Account 2 has sessions from different IPs but they don't overlap in time, so it's not banned.

Example 2 — No Overlapping Sessions
Input Table:
account_id ip_address login logout
1 1 2021-02-01 09:00:00 2021-02-01 11:00:00
1 2 2021-02-01 12:00:00 2021-02-01 14:00:00
2 1 2021-02-01 15:00:00 2021-02-01 17:00:00
Output:
account_id
💡 Note:

No accounts should be banned. Account 1 used two different IP addresses but the sessions were sequential (11:00 logout, 12:00 login) with no overlap. Account 2 only used one IP address.

Example 3 — Edge Case with Same IP
Input Table:
account_id ip_address login logout
1 1 2021-02-01 09:00:00 2021-02-01 12:00:00
1 1 2021-02-01 10:00:00 2021-02-01 11:00:00
2 1 2021-02-01 13:00:00 2021-02-01 15:00:00
Output:
account_id
💡 Note:

No accounts should be banned. Account 1 has overlapping sessions but they use the same IP address (1), which doesn't violate the rule. The rule only applies to simultaneous logins from different IP addresses.

Constraints

  • 1 ≤ account_id ≤ 10000
  • 1 ≤ ip_address ≤ 10000
  • login < logout for all rows
  • The table may contain duplicate rows

Visualization

Tap to expand
Leetflex Banned Accounts DetectionLogin SessionsAccount 1IP 1: 09:00-11:00IP 2: 10:00-12:00OVERLAP!Account 2IP 6: 13:00-15:00IP 7: 16:00-18:00No overlap - SafeBANNEDResult: Banned AccountsAccount 1Simultaneous loginfrom different IPsSelf-join detects overlapping time periods with different IP addresses
Understanding the Visualization
1
Input Data
Login sessions with timestamps and IP addresses
2
Self-Join
Compare sessions within same account
3
Filter Results
Find overlapping sessions with different IPs
Key Takeaway
🎯 Key Insight: Use self-join to compare login sessions and detect suspicious simultaneous access patterns
Asked in
Meta 15 Amazon 12 Google 8
25.4K Views
Medium Frequency
~18 min Avg. Time
892 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