90. Suspended Accounts
A company needs to detect fraudulent behavior by banning accounts that were simultaneously active from multiple IP addresses, indicating potential account sharing or hacking.
You are given LogInfo Table:
āāāāāāāāāāāāāāā¦āāāāāāāāāāā ā Column Name ā Type ā ā āāāāāāāāāāāāāā¬āāāāāāāāāā⣠ā account_id ā int ā āāāāāāāāāāāāāāā¼āāāāāāāāāāā ā ip_address ā int ā āāāāāāāāāāāāāāā¼āāāāāāāāāāā ā login ā datetime ā āāāāāāāāāāāāāāā¼āāāāāāāāāāā ā logout ā datetime ā āāāāāāāāāāāāāāā©āāāāāāāāāāā
- account_id: ID of the user/account logging in
- ip_address: IP address from which the account was logged in
- login: The timestamp when the user logged in
- logout: The timestamp when the user logged out
- The table may contain duplicate rows (same account_id, ip_address, login, and logout).
- It's confirmed that for every row, logout always happens after login.
Write a query to find the account_id of accounts that should be banned. An account should be banned if it has logged in at any moment from two different IP addresses. Return the result in any order.
Example 1:
Example:
Input:
āāāāāāāāāāāāāā¦āāāāāāāāāāāāā¦āāāāāāāāāāāāāāāāāāāāāā¦āāāāāāāāāāāāāāāāāāāāāā ā account_id ā ip_address ā login ā logout ā ā āāāāāāāāāāāāā¬āāāāāāāāāāāāā¬āāāāāāāāāāāāāāāāāāāāāā¬āāāāāāāāāāāāāāāāāāāāā⣠ā 1 ā 1 ā 2021-02-01 09:00:00 ā 2021-02-01 09:30:00 ā āāāāāāāāāāāāāā¼āāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāāā ā 1 ā 2 ā 2021-02-01 08:00:00 ā 2021-02-01 11:30:00 ā āāāāāāāāāāāāāā¼āāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāāā ā 2 ā 6 ā 2021-02-01 20:30:00 ā 2021-02-01 22:00:00 ā āāāāāāāāāāāāāā¼āāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāāā ā 2 ā 7 ā 2021-02-02 20:30:00 ā 2021-02-02 22:00:00 ā āāāāāāāāāāāāāā¼āāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāāā ā 3 ā 9 ā 2021-02-01 16:00:00 ā 2021-02-01 16:59:59 ā āāāāāāāāāāāāāā¼āāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāāā ā 3 ā 13 ā 2021-02-01 17:00:00 ā 2021-02-01 17:59:59 ā āāāāāāāāāāāāāā¼āāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāāā ā 4 ā 10 ā 2021-02-01 16:00:00 ā 2021-02-01 17:00:00 ā āāāāāāāāāāāāāā¼āāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāāāāāāāāāāāā ā 4 ā 11 ā 2021-02-01 17:00:00 ā 2021-02-01 17:59:59 ā āāāāāāāāāāāāāā©āāāāāāāāāāāāā©āāāāāāāāāāāāāāāāāāāāāā©āāāāāāāāāāāāāāāāāāāāāā
Expected Output:
āāāāāāāāāāāāāā ā account_id ā ā āāāāāāāāāāāā⣠ā 1 ā āāāāāāāāāāāāāā ā 4 ā āāāāāāāāāāāāāā
Explanation:
Account 1:
- Logged in from IP 1 (9:00ā9:30) and IP 2 (8:00ā11:30) overlapping = Ban.
Account 2:
- Different IPs, but sessions are on different days = Not banned.
Account 3:
- Different IPs, but no overlapping sessions = Not banned.
Account 4:
- Logged in from IP 10 (16:00ā17:00) and IP 11 (17:00ā17:59) with exact match at 17:00 Overlap = Ban.
Still unsure what the problem is asking ?
Letās go through a few more examples, step by step, to make it clearer.