cswork / libraryOpen practice workspace

Coding questions · Amazon

Generate Synthetic Clickstream Data with Python Function

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.

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.

Deterministic Synthetic Clickstream Event Generator

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 + 31u + 17d) % 10) events. For each event j in [0..k-1]: page_idx = (seed + 37u + 11d + 13j) % 5; offset = (seed + 97u + 101d + 103j) % 86400; clicked = 1 if ((seed + 131u + 137d + 139j) % 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)

Examples

Example 1

Input: num_users = 1, days = 1, seed = 0
Output: [{"user_id": 1, "event_ts": 1699913697, "page": "product", "clicked": 0}, {"user_id": 1, "event_ts": 1699913800, "page": "home", "clicked": 0}]

Explanation: For u=1,d=0: k=1+((0+311+170)%10)=2. j=0: page_idx=(0+371+110+130)%5=2=>product, offset=(0+971+1010+1030)%86400=97, clicked=(0+1311+1370+139*0)%100=31<15?0, ts=1699913600+97=1699913697. j=1: page_idx=0=>home, offset=200, clicked=(270%100)=70<15?0, ts=1699913800.

Example 2

Input: num_users = 1, days = 1, seed = 9
Output: [{"user_id": 1, "event_ts": 1699913706, "page": "search", "clicked": 0}]

Explanation: For u=1,d=0: k=1+((9+311+170)%10)=1. j=0: page_idx=(9+371)%5=1=>search, offset=(9+971)%86400=106, clicked=(9+131)%100=40<15?0, ts=1699913600+106=1699913706.

Hints

  1. Iterate days then users; compute k events per user per day using the provided modular formulas.
  2. Append dicts to a list and sort by (event_ts, user_id, page) before returning.
  3. Return an empty list when num_users <= 0 or days <= 0.

Approach

The function deterministically simulates events per user per day using simple modular arithmetic driven by the seed, avoiding external libraries. Each event is assigned a page, an in-day second offset, and a Bernoulli-like click using a <15% threshold on a 0..99 residue. We construct the full list and sort it by (event_ts, user_id, page) to guarantee a stable order.

  • Time complexity: O(N) where N is the total number of generated events
  • Space complexity: O(N)