InterviewStack.io LogoInterviewStack.io

Window Functions and Time Series Analytics Questions

Advanced SQL window functions: ROW_NUMBER, RANK, DENSE_RANK, LAG, LEAD, and aggregate functions (SUM, COUNT, AVG) with OVER and partition clauses. Using window functions to solve practical problems: ranking users or events within segments, calculating running totals and cumulative metrics, identifying trends and transitions over time, detecting patterns in user behavior sequences. Applications: cohort retention analysis (calculating retention rates across cohorts), user lifetime value trends, engagement metrics over time windows, and sequential user actions.

EasyTechnical
51 practiced
Using the purchases table above, show two SQL approaches to get each user's first purchase: (1) aggregation with MIN(purchased_at) GROUP BY user_id, and (2) a window function approach that returns the entire first purchase row (amount and purchase_id). Provide the queries and discuss pros and cons for performance and for retrieving additional columns from the first row.
MediumTechnical
66 practiced
Find top 3 customers by monthly order_value from orders(order_id, customer_id, order_value, order_date). Include ties such that if multiple customers tie for 3rd place they are all included. Write SQL using RANK or DENSE_RANK and explain why you chose one over the other.
MediumTechnical
60 practiced
Event monetization: events(user_id, event_time, plan) where plan in ('free','paid'). Write SQL to find each user's first_paid_date, i.e., the earliest event_time when plan changed to paid, but return null for users who never paid. Use window functions and consider users whose first event is already paid.
HardTechnical
60 practiced
Detect users who performed a sequence A -> B -> C within 30 days of the first event. Events table: events(user_id, event_type, occurred_at). For each qualifying user, return user_id and the first date when the full sequence completed. Provide a SQL solution using window functions and explain how it handles interleaving events and duplicates.
HardTechnical
48 practiced
Translate this SQL window-based logic into Spark DataFrame code: compute row_number per user ordered by event_time and a running sum of amount per user. Provide the DataFrame / Spark SQL snippet and discuss memory tuning, broadcast, and alternatives to reduce shuffle overhead in Spark.

Unlock Full Question Bank

Get access to hundreds of Window Functions and Time Series Analytics interview questions and detailed answers.

Sign in to Continue

Join thousands of developers preparing for their dream job.