Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

7-Day Rolling Resting Heart Rate

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

Your question is 7-Day Rolling Resting Heart Rate. 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

Whoop needs a daily view of resting heart rate trends for active members. Write a PostgreSQL query that calculates a 7-day rolling average for each member's recorded resting heart rate.

Requirements

  1. Join whoop_members to resting_heart_rate and return only active members.
  2. Exclude resting heart rate records where resting_hr is NULL.
  3. Calculate the average over the current date and preceding six calendar days, partitioned by member.
  4. Return results ordered by user_id and measurement date. Round the rolling average to two decimal places.

A missing measurement date should not be treated as a zero. The rolling average should use the qualifying observations that exist within the seven-day calendar window.

Schema

whoop_members
ColumnTypeDescription
user_idPKINTEGERUnique Whoop member identifier
member_nameVARCHAR(100)Member display name
membership_statusVARCHAR(20)Membership state, such as active or inactive
resting_heart_rate
ColumnTypeDescription
reading_idPKINTEGERUnique heart rate reading identifier
user_idINTEGERReferences whoop_members.user_id
measured_onDATEDate on which resting heart rate was measured
resting_hrNUMERIC(5,2)Resting heart rate in beats per minute
Tableswhoop_membersresting_heart_rate
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results