Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Trending in AI
Hot
Agentic AI EngineerForward-Deployed EngineerGenAI EngineerAI Architect
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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Start free
Validate Sort and Merge Transcript Segments
00:00
5 left

Validate Sort and Merge Transcript Segments

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

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

Sign up free to run or submit. Log in
Code
Sign up free to run your code. Your draft is saved.Run with a free accountLog inLn 2
Run your code to see test output here.