Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Detect Row-by-Row Balance Changes
00:00
5 left

Detect Row-by-Row Balance Changes

MediumSQL · PostgreSQL

Problem

You are given account transactions and need to produce a row-by-row balance audit for each account. Write a PostgreSQL query that shows each transaction with the prior balance, the balance change from the previous row, and the running balance after the transaction. The logic must work across records in timestamp order, so this is a good test of how you handle row-dependent calculations.

Schema

account_transactions
ColumnTypeDescription
transaction_idPKINTPrimary key for the transaction
account_idINTAccount identifier
transaction_tsTIMESTAMPTransaction timestamp used for sequencing
transaction_typeVARCHAR(20)Transaction category such as deposit or withdrawal
amountNUMERIC(12,2)Signed amount applied to the account
noteTEXTOptional note or reason for the transaction
Tablesaccount_transactions
Interviewer

Your question is Detect Row-by-Row Balance Changes. Start with the requirements and the one table 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.