Generate Synthetic Clickstream Data with Python Function
Company: Amazon
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Technical Screen
##### Scenario
The analytics team needs to generate synthetic click-stream records to test a new reporting pipeline before real traffic arrives.
##### Question
Write a Python function simulate_clickstream(num_users: int, days: int) that returns a Pandas DataFrame of simulated events with columns [user_id, event_ts, page, clicked]. Events should be timestamped within the past <days> days, each user should visit 1–10 random pages per day, and click probability is 0.15.
##### Hints
Use numpy.random for page counts and probabilities; build a list of dicts, then convert to DataFrame.
Quick Answer: This question evaluates a candidate's ability to implement synthetic data generation and probabilistic event simulation in Python using libraries like pandas and numpy, emphasizing timestamp handling, random sampling, and feature engineering for clickstream records.
Implement simulate_clickstream(num_users, days, seed=0) to generate a reproducible list of synthetic click-stream events. Return a list of dicts with keys [user_id, event_ts, page, clicked]. Use these rules: (1) Use BASE=1700000000 (Unix seconds) as a fixed 'now'. For each day d in [0..days-1], day_start = BASE - (d+1)*86400. (2) Pages = ["home", "search", "product", "cart", "checkout"]. (3) For each user u in [1..num_users] and each day d, generate k = 1 + ((seed + 31*u + 17*d) % 10) events. For each event j in [0..k-1]: page_idx = (seed + 37*u + 11*d + 13*j) % 5; offset = (seed + 97*u + 101*d + 103*j) % 86400; clicked = 1 if ((seed + 131*u + 137*d + 139*j) % 100) < 15 else 0; event_ts = day_start + offset. (4) Sort the final list by (event_ts asc, user_id asc, page asc). Return [] if num_users <= 0 or days <= 0.
Constraints
- 0 <= num_users <= 1000
- 0 <= days <= 30
- Each user generates 1..10 events per day
- event_ts is in [BASE - days*86400, BASE)
- Return events sorted by (event_ts, user_id, page)
Hints
- Iterate days then users; compute k events per user per day using the provided modular formulas.
- Append dicts to a list and sort by (event_ts, user_id, page) before returning.
- Return an empty list when num_users <= 0 or days <= 0.