Running Balance

MediumSQLSQL

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Table transactions: id INTEGER primary key; ids increase in the order transactions happened account_id INTEGER amount INTEGER positive for a deposit, negative for a withdrawal Return every transaction with the account's balance after it. Each account starts at 0. Columns, in this order: account_id, transaction id, amount, balance after this transaction. Order by account_id, then transaction id.

Examples

Input: transactions id | account_id | amount 1 | 1 | 100 2 | 2 | 50 3 | 1 | -30
Output: 1 | 1 | 100 | 100 1 | 3 | -30 | 70 2 | 2 | 50 | 50

Constraints

  • Balances can go negative.
  • Accounts are independent of each other.

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More SQL problems