Simulate optimistic locking: apply pending product updates only when the version matches
Company: Revolut
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Optimistic locking lets writers proceed without holding locks: each writer remembers the version of the row it read, and its change is applied only if the row still has that version when it writes.
Simulate optimistic locking: for each product, apply its pending update only if `expected_version` matches the current `version`. Return `product_id`, `product_name`, `price`, `stock_quantity`, `version`, `new_price`, `new_stock_quantity`, `new_version`, `update_status`, ordered by `product_id` ascending.
Requirements:
- No pending update: values unchanged, `update_status` = `'NO_UPDATE_REQUESTED'`.
- Version match: `new_price` = `price + price_adjustment`, `new_stock_quantity` = `stock_quantity + stock_adjustment`, `new_version` = `version + 1`, status `'SUCCESS'`.
- Version mismatch: values unchanged, status `'VERSION_CONFLICT'`.
"Values unchanged" means `new_price`, `new_stock_quantity` and `new_version` repeat the current `price`, `stock_quantity` and `version`.
```hint Rows with no update
Every product must appear. Work out what your version comparison evaluates to for a product that has no row in `pending_updates`.
```
### Input Table(s)
`products` (primary key `product_id`)
| column | type | meaning |
|---|---|---|
| product_id | INT | Product identifier |
| product_name | VARCHAR | Product name |
| price | DECIMAL(10,2) | Current price |
| stock_quantity | INT | Current units in stock |
| version | INT | Current row version, starting at 1 |
`pending_updates` (primary key `product_id`, which references `products`)
| column | type | meaning |
|---|---|---|
| product_id | INT | Product the update targets |
| expected_version | INT | Version the writer read before preparing the update |
| price_adjustment | DECIMAL(10,2) | Amount to add to the price (may be negative) |
| stock_adjustment | INT | Units to add to the stock (may be negative) |
### Output Contract
- Columns, in this order: `product_id`, `product_name`, `price`, `stock_quantity`, `version`, `new_price`, `new_stock_quantity`, `new_version`, `update_status`.
- One row per product in `products`, including products with no pending update.
- `update_status` is exactly one of `'NO_UPDATE_REQUESTED'`, `'SUCCESS'`, `'VERSION_CONFLICT'`.
- Rows are ordered by `product_id` ascending.
### Example
`products`
| product_id | product_name | price | stock_quantity | version |
|---|---|---|---|---|
| 1 | Laptop | 999.99 | 10 | 3 |
| 2 | Mouse | 25.00 | 200 | 1 |
| 3 | Keyboard | 49.50 | 0 | 2 |
| 4 | Monitor | 189.00 | 15 | 5 |
`pending_updates`
| product_id | expected_version | price_adjustment | stock_adjustment |
|---|---|---|---|
| 1 | 3 | -100.00 | -2 |
| 3 | 1 | 5.00 | 20 |
| 4 | 5 | 0.00 | 5 |
Expected result:
| product_id | product_name | price | stock_quantity | version | new_price | new_stock_quantity | new_version | update_status |
|---|---|---|---|---|---|---|---|---|
| 1 | Laptop | 999.99 | 10 | 3 | 899.99 | 8 | 4 | SUCCESS |
| 2 | Mouse | 25.00 | 200 | 1 | 25.00 | 200 | 1 | NO_UPDATE_REQUESTED |
| 3 | Keyboard | 49.50 | 0 | 2 | 49.50 | 0 | 2 | VERSION_CONFLICT |
| 4 | Monitor | 189.00 | 15 | 5 | 189.00 | 20 | 6 | SUCCESS |
Products 1 and 4 expect the version they currently have, so both updates apply and the version goes up by one. Product 3's writer read version 1, but the row is already at version 2, so the update is rejected. Product 2 has no pending update.
### Constraints and Clarifications
- `products` has at most 100 rows. `pending_updates` has at most one update per `product_id`, and every update refers to an existing product.
- `version` values are positive integers starting from 1. `expected_version` and both adjustments are never NULL (an adjustment of 0 means no change).
- `price` is positive and `stock_quantity` non-negative. Adjustments may be negative but never make `new_price` or `new_stock_quantity` negative.
- Any difference between `expected_version` and `version` is a conflict, whether the expected version is lower or higher.
Overview: Write a SQL query that simulates optimistic locking by applying each product's pending price and stock update only when its expected version matches the current version. It tests LEFT JOIN coverage, CASE logic and how NULL comparisons behave for products with no matching update.