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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Start free
7-Day Rolling Transactions
00:00
5 left

7-Day Rolling Transactions

MediumSQL · PostgreSQL

Problem

Featurespace's ARIC Risk Hub analyzes transaction activity by user. Write a PostgreSQL query that calculates a 7-day rolling average of daily transaction counts for each active user during the reporting period from 2024-01-01 through 2024-01-10.

Requirements

  1. Generate one row for every active user and every calendar day in the reporting period, including days with no transactions.
  2. Count transactions per user per day, treating missing activity as zero.
  3. Use a window function partitioned by user and ordered by date to calculate the rolling average over the current day and previous six calendar days.
  4. Return only dates from 2024-01-04 onward, ordered by user and activity date.

Output details

  • Return columns in this order: user_id, activity_date, daily_transaction_count, rolling_7_day_average.
  • Order results by user_id ascending, then activity_date ascending.
  • Round rolling_7_day_average to four decimal places.
  • Define active users as distinct non-NULL user_id values with transactions from 2024-01-01 00:00:00 inclusive through 2024-01-11 00:00:00 exclusive.

Schema

users
ColumnTypeDescription
user_idPKINTEGERUnique user identifier
user_nameVARCHAR(100)User display name
risk_segmentVARCHAR(30)Featurespace risk classification
emailVARCHAR(150)Contact email
transactions
ColumnTypeDescription
transaction_idPKINTEGERUnique transaction identifier
user_idINTEGERUser associated with the transaction
transaction_timestampTIMESTAMPTransaction event timestamp
channelVARCHAR(30)Transaction channel
amountNUMERIC(12,2)Transaction amount
Tablesuserstransactions
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

Sign up free to run or submit. Log in
CodePostgreSQL
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 1
Run your query to see results here.