Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Utility Function With Edge Cases

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

Your question is Utility Function With Edge Cases. 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

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):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output