Your question is Detecting Multi-Device Login Anomalies. Start with the requirements and the two tables on the right.
Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.
TikTok Trust and Safety needs to identify accounts with simultaneous logins from different device types and measure each account's total active session time without double-counting overlapping intervals. Write a PostgreSQL query using the provided tables.
ended_at is not null.| Column | Type | Description |
|---|---|---|
| user_idPK | INT | TikTok user identifier |
| username | VARCHAR(50) | TikTok account username |
| country_code | CHAR(2) | User country code |
| Column | Type | Description |
|---|---|---|
| session_idPK | INT | Login session identifier |
| user_id | INT | References users.user_id |
| device_type | VARCHAR(20) | Device category used for login |
| started_at | TIMESTAMP | Session start timestamp |
| ended_at | TIMESTAMP | Session end timestamp |