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 INPUT: LogInfo Table acc_id ip login logout 1 1.1.1.1 D1 D5 1 2.2.2.2 D2 D3 2 3.3.3.3 D1 D2 2 3.3.3.3 D3 D4 Session Timeline (Account 1) D1 D2 D3 D4 D5 IP: 1.1.1.1 IP: 2.2.2.2 OVERLAP! ALGORITHM STEPS 1 Self-Join Table Join LogInfo with itself on same account_id 2 Filter Different IPs WHERE a.ip != b.ip 3 Check Time Overlap a.login BETWEEN b.login AND b.logout 4 Select Distinct IDs Return unique banned account_ids SELECT DISTINCT a.account_id FROM LogInfo a, LogInfo b WHERE a.account_id = b.account_id AND a.ip != b.ip AND a.login BETWEEN b.login AND b.logout FINAL RESULT Banned Accounts Found: account_id 1 Account 1 Banned Because: Logged in from 1.1.1.1 AND 2.2.2.2 simultaneously during D2-D3 Account 2: OK Same IP, no overlap issue Output: [1] Key Insight: Two sessions overlap when one's login time falls within the other's login-logout range. Self-join allows comparing each session with all other sessions of the same account. Time Complexity: O(n^2) for self-join | Space Complexity: O(n) for distinct results TutorialsPoint - Leetflex Banned Accounts | Optimal Solution (Self-Join with Overlap Detection)
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