Calculate Average Event Value by User ID
UpstartData Scientist · Technical ScreenLast updated
This question evaluates a candidate's skill in group-wise aggregation and data transformation on JSON-like event streams, specifically computing per-user average metrics and producing ranked results.
events
+----+---------+------------+-------+---------------------+ | id | user_id | event_type | value | timestamp | +----+---------+------------+-------+---------------------+ | 1 | 42 | click | 3.5 | 2024-03-10 10:00:00 | | 2 | 17 | view | 1.0 | 2024-03-10 10:01:05 | | 3 | 42 | purchase | 7.0 | 2024-03-10 10:02:30 | | 4 | 99 | click | 2.3 | 2024-03-10 10:04:11 | | 5 | 17 | purchase | 4.7 | 2024-03-10 10:05:45 | +----+---------+------------+-------+---------------------+
Scenario
You receive an API response that is a Python list of JSON objects, each describing a user event coming from a mobile-app funnel.
Question
Write Python code that, given a list like events = [{"id":1,"user_id":42,"event_type":"click","value":3.5,"timestamp":"2024-03-10 10:00:00"}, ...], produces a dictionary mapping each user_id to the average value of their events. Return the result sorted by descending average value.
Hints
Use defaultdict / pandas groupby; iterate only once if possible.
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.