Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Rolling Average Outage Duration
00:00
5 left

Rolling Average Outage Duration

MediumSQL · PostgreSQL

Problem

PG&E reliability analysts need to monitor how outage duration changes over time across service areas. Write a PostgreSQL query that calculates the rolling average duration of outages for each mapped PG&E service area.

Use a 30-day inclusive window, meaning each outage includes qualifying outages from the current timestamp and the previous 29 days. Exclude outage events that do not map to a known service area.

Requirements

  1. Join outage_events to service_areas using service_area_id.
  2. Calculate each outage duration in hours from started_at and ended_at; preserve NULL durations when an outage has not ended.
  3. Use a window function partitioned by service area and ordered by started_at to calculate the rolling average.
  4. Round individual durations and rolling averages to two decimal places, and order the output by service area and start time.

Schema

outage_events
ColumnTypeDescription
outage_idPKINTEGERUnique outage event identifier
service_area_idINTEGERReferences the PG&E service area
started_atTIMESTAMPOutage start timestamp
ended_atTIMESTAMPOutage end timestamp
causeVARCHAR(100)Reported outage cause
service_areas
ColumnTypeDescription
service_area_idPKINTEGERUnique service area identifier
service_areaVARCHAR(100)PG&E service area name
Tablesoutage_eventsservice_areas
Interviewer

Your question is Rolling Average Outage Duration. Start with the requirements and the two tables in the Question tab.

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.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.