← Upstart Interview Insights

Upstart·Data Scientist·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

Upstart Data Scientist interview with a Python coding question built around clickstream data. Pretty much one meaty technical problem with some complexity analysis tacked on at the end.

Questions Asked (1)

Q1

Write a Python function that takes a list of event dictionaries (each with user_id, event_type, revenue, and timestamp) and returns per-user total revenue and average session duration in seconds. Walk through the time and space complexity of your solution.

Algorithms & Data StructuresProduct Analytics & Metrics
Author's notes

I went straight for defaultdict and did a single pass, tracking min and max timestamps per user to get session duration.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the definition of a session and how to compute its duration from the event timestamps. Then, design a solution that groups events by user, computes total revenue by summing revenue values, and calculates average session duration by identifying session boundaries (e.g., gaps > 30 minutes) and averaging the durations. Finally, analyze the time and space complexity, typically O(n) time and O(u) space where u is the number of users.

Pro tip: Mention that session duration is often defined with a timeout threshold (e.g., 30 minutes of inactivity), and that you would confirm this assumption with the interviewer. Also, note that you can compute both metrics in a single pass over the data to optimize performance.

1. Clarify Requirements and Assumptions

Ask clarifying questions about the definition of a session (e.g., inactivity timeout) and whether events are sorted by timestamp. Confirm the expected output format (e.g., dictionary mapping user_id to total revenue and average session duration).

2. Design the Algorithm

Outline a plan: group events by user_id, sort each user's events by timestamp, then iterate through to compute total revenue and identify sessions based on time gaps. For each session, compute its duration (last timestamp - first timestamp) and accumulate for average.

3. Implement the Function

Write clean Python code using a dictionary to store per-user data. For each user, maintain a list of events, then process to compute total revenue and session durations. Return a dictionary with user_id as keys and a tuple or dict of metrics as values.

4. Analyze Complexity

Explain that the time complexity is O(n log n) if sorting is needed per user (or O(n) if events are already sorted), and space complexity is O(n) for storing events. Discuss potential optimizations if data is large.

5. Test and Validate

Walk through a small example to verify correctness, including edge cases like users with a single event (session duration 0) and events with zero revenue. Mention that you would write unit tests.

Key Points to Mention

  • Definition of a session: typically a period of activity separated by a timeout (e.g., 30 minutes of inactivity).
  • Handling unsorted timestamps: sorting per user or using a heap if streaming.
  • Time complexity: O(n log n) due to sorting, but can be O(n) if timestamps are pre-sorted.
  • Space complexity: O(n) to store events, or O(u) if aggregating on the fly with sorted input.
  • Edge cases: users with no events, single event, or multiple sessions.
  • Potential for single-pass aggregation if events are sorted by user and timestamp.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.