Coding question · Date TruncHardAsked at Uber

Longest dormancy between orders per customer

LAG window function + MAX aggregation to find the longest gap.

Back to all coding questions

The Brief

Retail AnalyticsCRM team#coding-interview

For each customer, find the longest gap (in days) between any two consecutive orders. Columns: `customer_id`, `longest_gap_days`. Order by longest_gap_days descending. Table: `ci_orders`.

Tables & columns available

ci_ordersfact6 columns
ColumnTypeKey
order_idINTPK
customer_idINT
product_nameTEXT
categoryTEXT
order_dateDATE
revenueNUMERIC

Hints (3)

Each hint you reveal reduces the XP you can earn. Try the query first.

Hint 1

LAG(order_date) OVER (PARTITION BY customer_id ORDER BY order_date) to get each order's previous order date.

Hint 2

Wrap in a CTE, then GROUP BY customer_id and MAX(order_date - prev_date).

Hint 3

Customers with only one order have no gap — filter WHERE prev_date IS NOT NULL in the outer query before aggregating.