Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
CSV Duration Calculation
00:00
5 left

CSV Duration Calculation

MediumPython

Problem

Given two CSV files of transaction start and complete events (transaction ID, user, timestamp), calculate each transaction duration, accounting for starts without a completion and duplicate transactions for the same user.

Asked in the phone screen stage. OP states the same question was given at their phone screen.

Implement calculate_transaction_durations(starts, completes). Each argument is a list of dictionaries with transaction_id, user, and integer timestamp. Rows in each list are chronological. Match completions to the earliest unmatched start with the same transaction ID and user. Return one dictionary per start, preserving start order, with transaction_id, user, start_timestamp, complete_timestamp, and duration. Use None for both completion and duration when no completion exists. Ignore completions without a matching start.

Examples: [{"transaction_id":"t1","user":"u1","timestamp":10}] and [{"transaction_id":"t1","user":"u1","timestamp":25}] produce duration 15. Two starts for the same key and one completion leave the second start unmatched.

Constraints: At most 100,000 rows per file; timestamps are nonnegative integers.

Constraints

  • 0 <= len(starts), len(completes) <= 100000
  • Each event contains transaction_id, user, and timestamp
  • transaction_id and user are non-empty strings
  • timestamps are nonnegative integers
  • Rows in each input list are sorted by nondecreasing timestamp
  • A completion matches the earliest unmatched start with the same transaction ID and user

Function Signature

def calculate_transaction_durations(starts, completes):
Interviewer

Your question is CSV Duration Calculation. Start with the requirements in the Question tab.

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.
Code
Sign up free to run your codeLog inLn 2
Run your code to see test output here.