Medium · Database
Latest Order Per Customer
Given Orders(id, customerId, placedAt), return CustomerId, OrderId, and PlacedAt for each customer’s latest order. Dates are ISO YYYY-MM-DD strings; break same-day ties by larger order ID. Row order does not matter.
Examples
Example 1
{
"orders": [
{"id": 10, "customerId": 1, "placedAt": "2026-01-01"},
{"id": 11, "customerId": 1, "placedAt": "2026-01-03"},
{"id": 12, "customerId": 1, "placedAt": "2026-01-03"},
{"id": 20, "customerId": 2, "placedAt": "2026-01-02"}
]
}Output: [{"CustomerId":1,"OrderId":12,"PlacedAt":"2026-01-03"},{"CustomerId":2,"OrderId":20,"PlacedAt":"2026-01-02"}]
Example 2
{
"orders": [
{"id": 99, "customerId": 7, "placedAt": "2025-12-31"},
{"id": 3, "customerId": 7, "placedAt": "2026-01-01"}
]
}Output: [{"CustomerId":7,"OrderId":3,"PlacedAt":"2026-01-01"}]
Rebuild it in the studio
20 free core interview problems with full runner & lessons; explore the complete 380+ library with Pro.