Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Court Time Calculation

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

Your question is Court Time Calculation. 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

Hudl Sportscode records player substitutions as timestamped events during a match. Given the players on court at the start, substitution events, and the final match timestamp, calculate the total number of seconds each player was on the court.

Formal Specification

Implement calculate_court_time(initial_players, events, end_time).

  • initial_players is a list of player ID strings on court at timestamp 0.
  • events is a list of events represented as [timestamp, player_id, action], where action is either "in" or "out".
  • Events may be in any order. Events with the same timestamp have no meaningful ordering because no time passes between them.
  • end_time is the nonnegative integer timestamp when the match ends.
  • Return a dictionary mapping every player appearing in initial_players or events to their total court time in seconds. Players who never enter must have a total of 0.

Assume all timestamps are integers in [0, end_time], the input describes valid substitutions, and a player cannot be both on and off the court at the same time.

Constraints

  • 1 <= len(initial_players) <= 10
  • 0 <= len(events) <= 10^5
  • 0 <= timestamp <= end_time <= 10^9
  • Each event has the form [timestamp, player_id, action].
  • The action is either "in" or "out".
  • Events describe valid substitutions.
  • Events at the same timestamp are simultaneous.

Function Signature

def calculate_court_time(initial_players, events, end_time):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output