Cheapest Way to Concatenate Encoded Video Segments Into One File

Last updated: November 29, 2026

Quick Overview

Concatenating two video segment files costs the sum of their sizes, and the result becomes a new segment. Return the minimum total cost to combine all segments into one file.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


Concatenating two video segment files costs the sum of their sizes, and the result becomes a new segment. Return the minimum total cost to combine all segments into one file.

A Meta phone-screen warm-up set in the video processing stack. The interviewer wants the Huffman-style greedy (always merge the two smallest) and a short reason why big segments should be merged as late as possible.

What the Interviewer Expects
  • Recognize the greedy of merging the two smallest each time
  • Use a min-heap for repeated extraction
  • Explain that each size is paid once per merge it participates in
  • Handle zero or one segment
  • State O(n log n)
Key Topics to Cover
greedy
min-heap
Huffman coding
merge cost
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Prove the greedy is optimal.
  • What if you may merge three segments at once?
  • What if only adjacent segments can be merged?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: list of positive segment sizes.
  • Any two segments can be merged; order of segments in the final file does not matter for cost.
  • Output: minimum total merge cost; 0 if there are fewer than t...
High-Level Design

Every merge pays the full size of both inputs, and a segment's bytes get paid again every time the file containing it is merged. So the total cost equals the sum over segments of size * depth in the...


Submit Your Answer
Markdown supported