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
Utility Function With Edge Cases
00:00
5 left

Utility Function With Edge Cases

MediumPython

Problem

Descript stores transcript pieces as timed segments. Implement a utility that validates these segments, orders them by start time, and merges consecutive segments from the same speaker when the gap between them is no greater than max_gap.

Formal Specification

Implement merge_transcript_segments(segments, max_gap). segments is a list of dictionaries with these keys:

  • start: non-negative number representing the start time in seconds
  • end: number greater than or equal to start
  • speaker: non-empty string
  • text: string

Return a new list of dictionaries in ascending start order. When two mergeable segments are combined, use the earliest start, the latest end, the same speaker, and join non-empty text values with one space. Segments merge when they have the same speaker and next.start <= current.end + max_gap.

Raise TypeError if segments is not a list, max_gap is not numeric, or a segment is not a dictionary with valid field types. Raise ValueError if max_gap is negative, a timestamp is negative, start > end, or speaker is empty. An empty input returns an empty list. Do not mutate the input list or its dictionaries.

Constraints

  • 0 <= len(segments) <= 10^5
  • Timestamps and max_gap must be finite numbers
  • 0 <= start <= end
  • speaker must be a non-empty string
  • text must be a string
  • The input list and dictionaries must not be mutated

Function Signature

def merge_transcript_segments(segments, max_gap):
Interviewer

Your question is Utility Function With Edge Cases. 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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.