Python Interview Prep/Average Session Length
Meta logo

Average Session Length

Meta·medium·pandas·About 25 min in the room

events logs what users did: user_id, timestamp (text) and action (page_load, scroll or page_exit). A user has at most one session a day. It runs from their LAST page_load of that day to the FIRST page_exit after it. A day with no page_exit after its last page_load has no session. Find each user's average session length in seconds. Return result with the columns user_id and avg_seconds, one row for every user who had at least one session.

The data

events24 rows
user_idint64timestampobjectactionobject
02025-04-01 09:00:00page_load
02025-04-01 09:03:20scroll
02025-04-01 09:10:00page_exit
02025-04-02 13:00:00page_load
02025-04-02 13:05:00page_load
02025-04-02 13:25:00page_exit
and 18 more rows

Target output

user_idavg_seconds
0900
11800
2900
4620
5900

What this question tests

Tests turning an event log into sessions: pairing a start event with the right end event, per user per day. The common shortcut, latest load and earliest exit of the day, gives negative sessions on a day that has two of them, and interviewers know it.

More Python interview questions

  • Weeks at Number One · Spotify · easy
  • The Salary Gap · LinkedIn · easy
  • Month-over-Month Revenue · Amazon · medium
  • Back Within a Week · Amazon · medium
Python
Shift+EnterPython is starting in your browser...
Output

Starting Python...