Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Seven-Day Revenue Per User Average

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Seven-Day Revenue Per User Average. Start with the requirements 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

On a Meta product such as Facebook, you are given raw daily monetization events and need to compute a smoothed revenue-per-user trend. Write a function that returns the 7-day rolling average of daily revenue per user (RPU) for each calendar day.

For each event, you are given a date string in YYYY-MM-DD format, a user_id, and a revenue amount. First aggregate events by day to compute:

  • daily revenue = sum of all revenue on that day
  • daily active users = number of distinct users with at least one event on that day
  • daily RPU = daily revenue / daily active users

Then compute the 7-day rolling average of daily RPU over the current day and the previous 6 days. If fewer than 7 days exist so far, average over all available days.

Return a list of [date, rolling_average] pairs in ascending date order. Round each rolling average to 2 decimal places.

Formal Specification

  • Input: events, a list of lists where each item is [date, user_id, revenue]
  • Output: a list of [date, rolling_average]

Constraints

  • 1 <= len(events) <= 10^5
  • date is a valid YYYY-MM-DD string
  • user_id is a non-empty string
  • 0 <= revenue <= 10^6
  • Multiple events may exist for the same user on the same day

Function Signature

def rolling_rpu_7day(events):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output