Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Database Consistency Under Concurrency
00:00
5 left

Database Consistency Under Concurrency

MediumSQL · PostgreSQL

Problem

Explain your strategy for database consistency in a highly concurrent environment.

Write a PostgreSQL query that audits stored account balances against their posted ledger entries. Treat credits as positive, debits as negative, ignore non-posted entries, and safely handle NULL amounts.

Output

  1. One row per inconsistent account, including account_id, currency, opening_balance, current_balance, expected_balance, and difference.
  2. Include only accounts whose stored balance differs from the calculated balance, ordered by account_id ascending.

Schema

accounts
ColumnTypeDescription
account_idPKINTUnique account identifier
currencyVARCHAR(3)ISO currency code
opening_balanceNUMERIC(18,2)Balance before ledger activity
current_balanceNUMERIC(18,2)Stored current account balance
ledger_entries
ColumnTypeDescription
entry_idPKINTUnique ledger entry identifier
account_idINTAccount affected by the entry
entry_typeVARCHAR(30)Ledger event type
amountNUMERIC(18,2)Monetary amount of the entry
statusVARCHAR(20)Processing status of the entry
Tablesaccountsledger_entries
Interviewer

Your question is Database Consistency Under Concurrency. Start with the requirements and the two 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
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.