Find the Latest Balance for a Bank Account
Company: Stripe
Role: Software Engineer
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Technical Screen
Write a PostgreSQL query that returns the latest reported balance for one bank account.
### Tables
`bank_balances(bank_name text, account_name text, start_date date, end_date date, balance numeric(18,2))`
`requested_account(bank_name text, account_name text)` contains exactly one row.
### Rules
- Match both bank name and account name exactly. Account names are text identifiers and may contain leading zeros.
- For this exercise, latest means greatest `end_date`; each bank/account pair has at most one row with a given end date.
- Every field is non-null, and `start_date <= end_date`.
- Records may span different months and years. Dates are typed dates, not strings to compare lexicographically.
### Output
Return one column named `balance` for the latest matching record. If the requested account has no record, return no rows. The end-date definition and missing-account behavior are explicit exercise conventions.
### Example
`bank_balances`:
| bank_name | account_name | start_date | end_date | balance |
|---|---|---|---|---:|
| Harbor | 001234 | 2021-12-01 | 2021-12-31 | 150.00 |
| Harbor | 001234 | 2019-05-01 | 2019-05-31 | 200.00 |
| Bay | 001234 | 2022-01-01 | 2022-01-31 | 300.00 |
`requested_account`: `('Harbor', '001234')`
Expected output:
| balance |
|---:|
| 150.00 |
Overview: Query the latest dated bank-account balance using both bank and account identifiers while handling records across months and years.
Read the full Stripe Software Engineer interview experience this question came from
Write a PostgreSQL query that returns the latest reported balance for one bank account.
Tables:
- bank_balances(bank_name text, account_name text, start_date date, end_date date, balance numeric(18,2))
- requested_account(bank_name text, account_name text), which contains exactly one row.
Rules:
- Match both bank_name and account_name exactly against the row in requested_account. Account names are text identifiers and may contain leading zeros.
- Latest means the greatest end_date; each bank_name/account_name pair has at most one row with a given end_date.
- Every field is non-null, and start_date <= end_date.
- Records may span different months and years. Dates are typed dates, not strings to compare lexicographically.
Output: return one column named balance for the latest matching record. If the requested account has no record, return no rows.
Tables
bank_balances(bank_name TEXT, account_name TEXT, start_date DATE, end_date DATE, balance NUMERIC(18,2))
requested_account(bank_name TEXT, account_name TEXT)
Hints
- The requested account is identified by both bank_name and account_name; the same account_name can appear at another bank.
- Treat account_name as text: '001234' and '1234' are different accounts.