Latest Price Record per Cryptocurrency from a Time-Series Ledger
Company: Coinbase
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
A price ledger stores snapshots of cryptocurrency prices over time. `cryptocurrencies` has one row per asset, and `price_ledger` has many rows per asset, one for each recorded snapshot.
Return the latest price record for each cryptocurrency with columns `crypto_id`, `symbol`, `Name`, `LatestPrice`, `RecordedAt`, `Volume24h`. If multiple records share the latest `recorded_at` for a cryptocurrency, return all of them.
```hint Ties at the latest timestamp
Some common ways of keeping one row per group return exactly one row even when two rows tie. Make sure your method keeps every tied entry.
```
### Input Table(s)
`cryptocurrencies` (primary key `crypto_id`)
| Column | Type | Meaning |
|---|---|---|
| `crypto_id` | INT | Asset identifier, primary key |
| `symbol` | VARCHAR(20) | Ticker symbol such as `BTC`; not guaranteed unique |
| `name` | VARCHAR(100) | Full asset name |
`price_ledger` (primary key `entry_id`; `crypto_id` references `cryptocurrencies.crypto_id`)
| Column | Type | Meaning |
|---|---|---|
| `entry_id` | BIGINT | Ledger entry identifier, primary key |
| `crypto_id` | INT | Asset the snapshot belongs to |
| `price` | DECIMAL(18,2) | Price at `recorded_at` |
| `volume_24h` | DECIMAL(20,2) | Trading volume over the 24 hours before `recorded_at` |
| `recorded_at` | TIMESTAMP | When the snapshot was recorded |
### Output Contract
- Columns, in this order: `crypto_id`, `symbol`, `Name`, `LatestPrice`, `RecordedAt`, `Volume24h`. The names are case-sensitive, so in PostgreSQL the last four are double-quoted aliases (`AS "Name"`).
- `Name` is `cryptocurrencies.name`, `LatestPrice` is `price`, `RecordedAt` is `recorded_at` and `Volume24h` is `volume_24h`, all returned unchanged.
- One row per ledger entry whose `recorded_at` equals the latest `recorded_at` of its cryptocurrency.
- Order by `symbol` ascending, then `crypto_id` ascending, then `entry_id` ascending. `entry_id` only breaks ties between entries of the same cryptocurrency at the same timestamp; it is not an output column.
### Example
`cryptocurrencies`
| crypto_id | symbol | name |
|---|---|---|
| 1 | BTC | Bitcoin |
| 2 | ETH | Ethereum |
| 3 | SOL | Solana |
| 4 | ADA | Cardano |
`price_ledger`
| entry_id | crypto_id | price | volume_24h | recorded_at |
|---|---|---|---|---|
| 101 | 1 | 64000.00 | 1200.50 | 2024-05-01 10:00:00 |
| 102 | 1 | 64250.00 | 1310.00 | 2024-05-01 11:00:00 |
| 103 | 2 | 3100.00 | 8800.00 | 2024-05-01 11:00:00 |
| 104 | 2 | 3105.50 | 8900.00 | 2024-05-01 11:30:00 |
| 105 | 2 | 3104.00 | 8950.00 | 2024-05-01 11:30:00 |
| 106 | 3 | 145.20 | 51000.00 | 2024-05-01 09:00:00 |
Expected result:
| crypto_id | symbol | Name | LatestPrice | RecordedAt | Volume24h |
|---|---|---|---|---|---|
| 1 | BTC | Bitcoin | 64250.00 | 2024-05-01 11:00:00 | 1310.00 |
| 2 | ETH | Ethereum | 3105.50 | 2024-05-01 11:30:00 | 8900.00 |
| 2 | ETH | Ethereum | 3104.00 | 2024-05-01 11:30:00 | 8950.00 |
| 3 | SOL | Solana | 145.20 | 2024-05-01 09:00:00 | 51000.00 |
Bitcoin's latest snapshot is entry 102 at 11:00. Ethereum has two entries (104 and 105) at its latest timestamp, 11:30, so both appear in `entry_id` order. Cardano has no ledger entries and is absent, and the rows sort by symbol: BTC, ETH, SOL.
### Constraints and Clarifications
- There are 1 to 10,000 cryptocurrencies and 1 to 1,000,000 ledger entries.
- Every `price_ledger.crypto_id` references an existing cryptocurrency. A cryptocurrency with no ledger entries has no latest record and does not appear.
- No ledger column is NULL.
- "Latest" is per cryptocurrency: each asset's own maximum `recorded_at`. Different assets can have different latest timestamps.
- Two cryptocurrencies may share a `symbol`; `crypto_id` then decides their order.
- The same timestamp can appear for different cryptocurrencies; a shared timestamp does not link their rows.
Overview: A SQL problem on a time-series price ledger: return the most recent price record for every cryptocurrency, keeping every entry that ties at the latest timestamp. It tests the latest-row-per-group pattern, joining an aggregate back to the detail table, and deterministic ordering.