Skip to content

49. Conversion Funnel

Difficulty: Hard · Topics: Aggregations, Conditional Logic

The events table has user_id and step ('visit', 'signup', 'purchase'; users may repeat steps). A user counts at a step only if they also completed every earlier step.

Return a single row: visits, signups, purchases (user counts) and signup_rate and purchase_rate (percent of the previous step, 1 decimal; null if the previous step is 0).

Row order does not matter; column names must match. Your code is graded on 3 test cases, including hidden edge cases.

Sample data

events

user_idstep
1visit
1signup
1purchase
2visit
2signup
3visit
4visit
4visit

Expected output

visitssignupspurchasessignup_ratepurchase_rate
4215050

Hints

Hint 1First reduce to one row per user with a 0/1 flag per step: F.max(F.when(step == "visit", 1).otherwise(0)).
Hint 2A user reaches signup only if visit AND signup: multiply the flags.
Hint 3Then sum the flags across users and compute the rates.

PySpark functions you'll practise

Related problems

Browse

Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings

Difficulty: Easy · Medium · Hard · PySpark interview roadmap · All problems