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
Get started
Scalable Transaction Logging Schema
00:00
5 left

Scalable Transaction Logging Schema

HardSQL · PostgreSQL

Problem

How would you design a scalable database schema to handle high-volume transaction logging and reporting requirements? Use the provided schema to produce a monthly report for active Mastercard merchants from January through March 2025, considering only transactions whose latest event is SETTLED.

Output

  1. One row per active merchant and reporting month, including months with no settled transactions
  2. Columns: merchant_id, merchant_name, report_month, transaction_count, total_amount, prior_month_amount, month_over_month_pct, and cumulative_amount
  3. Order by merchant_id, then report_month ascending

Output details

  • Use 2025-01-01 through 2025-03-31 inclusively, with timestamps on or after 2025-01-01 00:00:00 and before 2025-04-01 00:00:00.
  • Represent each report_month as the first day of its month: 2025-01-01, 2025-02-01, and 2025-03-01.
  • For each transaction, use the event with the greatest event_id as its latest event, and include it only when that event is SETTLED.
  • Format total_amount and cumulative_amount as numeric values with exactly two decimal places, replacing missing totals with 0.00.
  • Set prior_month_amount to NULL for the first month, and calculate it as the preceding reporting month's total_amount.
  • Return month_over_month_pct rounded to two decimals, but use NULL when prior_month_amount is NULL or zero.

Schema

merchants
ColumnTypeDescription
merchant_idPKINTUnique Mastercard merchant identifier
merchant_nameVARCHAR(120)Merchant display name
merchant_categoryVARCHAR(60)Merchant business category
is_activeBOOLEANWhether the merchant is active for reporting
transaction_log
ColumnTypeDescription
transaction_idPKBIGINTUnique transaction identifier
merchant_idINTMerchant associated with the transaction
created_atTIMESTAMPTransaction creation timestamp
amountNUMERIC(12,2)Transaction amount in the transaction currency
currencyVARCHAR(3)ISO 4217 currency code
transaction_events
ColumnTypeDescription
event_idPKBIGINTMonotonically increasing event identifier
transaction_idBIGINTTransaction whose state changed
event_typeVARCHAR(30)Transaction lifecycle state
event_atTIMESTAMPTime at which the event occurred
Tablesmerchantstransaction_logtransaction_events
Interviewer

Your question is Scalable Transaction Logging Schema. Start with the requirements and the three 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.