Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Detecting Multi-Device Login Anomalies

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

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.

You need to log in / sign up to run or submit.

Problem

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.

Requirements

  1. Consider only completed sessions where ended_at is not null.
  2. Flag users with at least one pair of overlapping sessions from different devices. Treat a null device value as different from a non-null device value.
  3. Merge overlapping or touching session intervals for each user before calculating total duration.
  4. Return anomalous users with their username, number of distinct cross-device overlapping pairs, and total active minutes, ordered by total active minutes descending.

Schema

users
ColumnTypeDescription
user_idPKINTTikTok user identifier
usernameVARCHAR(50)TikTok account username
country_codeCHAR(2)User country code
login_sessions
ColumnTypeDescription
session_idPKINTLogin session identifier
user_idINTReferences users.user_id
device_typeVARCHAR(20)Device category used for login
started_atTIMESTAMPSession start timestamp
ended_atTIMESTAMPSession end timestamp
Tablesuserslogin_sessions
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results