Revenue per customer (like an aggregate)

A code exercise of about 15 minutes: read the statement, solve «Revenue per customer (like an aggregate)» in the browser and get a review like in a real interview.

Code · 15 min
Loading…

Statement

Write revenueByCustomer(orders). Each order has customerId, total (cents) and status. Only paid orders count. Return [{ customerId, orders, revenue }] sorted by revenue descending (ties: customerId ascending). It's what you'd do in MongoDB with $match + $group + $sort: also write that aggregation pipeline in the comment at the end (it doesn't run, but the review takes it into account).