N-th Most Recent Purchase per User and Its Change from the Previous Purchase

Asked at Coinbase · ML Fundamentals & Algorithms · Difficulty: Medium · Last asked Dec 19, 2025

An analytics team keeps every user action in one `events` table. They want, for each user, the purchase that sits `n` places back from that user's latest purchase, together with how much its amount changed compared with the purchase just before it. Write the SQL query that produces this report.

Loading the problem…