Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Python Average Session Duration

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Python Average Session Duration. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

EA SPORTS FC gameplay telemetry contains one record for each completed user session. Write a function that calculates the average session duration across all provided sessions.

Each session is represented by a dictionary containing a user_id, start, and end. The start and end values are integer timestamps measured in minutes from a common reference point. The average should include every session, regardless of whether multiple sessions belong to the same user.

Return 0.0 when the input contains no sessions.

Formal Specification

Implement average_session_duration(sessions).

  • Input: sessions, a list of dictionaries. Each dictionary has:
    • user_id: a string
    • start: an integer start timestamp
    • end: an integer end timestamp
  • Output: A floating-point number equal to the mean of end - start for all sessions.

Constraints

  • 0 <= len(sessions) <= 10^5
  • 0 <= start <= end <= 10^9
  • Every session contains user_id, start, and end
  • Timestamps are integer minute values

Function Signature

def average_session_duration(sessions):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output