Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling Active Usage and Anomalies

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

Your question is Rolling Active Usage and 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

Given a large table of device diagnostic logs, write a query to compute rolling 30-day active usage rates and flag anomalies.

Treat a device as active on a date when it has at least one log with positive usage minutes. The rate is the number of active days in the inclusive trailing 30-day period divided by 30. Exclude logs outside a device's active lifetime and flag rates outside the device's expected range.

Output

  1. One row per valid device and observed log date.
  2. Columns: device_id, observation_date, active_usage_rate, and anomaly_flag.
  3. Sort by device_id, then observation_date.
  4. Return TRUE for anomaly_flag when the rate is below the minimum or above the maximum expected rate.

Schema

devices
ColumnTypeDescription
device_idPKINTUnique device identifier
product_modelVARCHAR(100)Apple device model
activated_onDATEDate the device became active
retired_onDATEDate the device was retired
minimum_expected_rateNUMERIC(5,4)Minimum expected 30-day active usage rate
maximum_expected_rateNUMERIC(5,4)Maximum expected 30-day active usage rate
diagnostic_logs
ColumnTypeDescription
log_idPKINTUnique diagnostic log identifier
device_idINTReferenced device identifier
log_dateDATEDate the diagnostic log was recorded
usage_minutesINTUsage minutes recorded by the diagnostic log
event_typeVARCHAR(50)Diagnostic event classification
Tablesdevicesdiagnostic_logs
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results