Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling Averages and Anomaly Flags

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

Your question is Rolling Averages and Anomaly Flags. 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

Amazon Services Operations Managers monitor hourly package throughput across fulfillment facilities. Write a PostgreSQL query that calculates a three-reading rolling average and identifies unusually high or low throughput.

Requirements

  1. Join throughput logs to Amazon fulfillment facility metadata.
  2. Calculate a rolling average using the current reading and up to the two preceding readings for the same facility, ordered by hour.
  3. Label a reading ANOMALY when it differs from the rolling average by more than 20%. Label rows with fewer than three non-null readings in the frame as INSUFFICIENT_DATA; otherwise label them NORMAL.
  4. Return results ordered by facility and hour. Round the displayed rolling average to two decimal places.

Schema

amazon_facilities
ColumnTypeDescription
facility_idPKINTAmazon fulfillment facility identifier
facility_nameVARCHAR(100)Facility code used by operations teams
regionVARCHAR(50)Geographic operating region
hourly_throughput_logs
ColumnTypeDescription
log_idPKINTUnique throughput log identifier
facility_idINTFacility associated with the measurement
logged_hourTIMESTAMPStart time of the hourly measurement
throughput_unitsINTPackages processed during the hour
Tablesamazon_facilitieshourly_throughput_logs
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results