Cheapest Way to Compact Adjacent Yahoo Mail Storage Segments

Last updated: November 29, 2026

Quick Overview

A mailbox's storage is a row of segments with known sizes. You may merge two adjacent segments at a cost equal to their combined size, and you repeat until one segment remains. Return the minimum total cost.

Yahoo
Coding & Algorithms
Software Engineer
Yahoo
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


A mailbox's storage is a row of segments with known sizes. You may merge two adjacent segments at a cost equal to their combined size, and you repeat until one segment remains. Return the minimum total cost.

Yahoo Mail stores huge volumes of message blobs, and compaction cost is real I/O. This is an interval DP question: the interviewer wants you to see why greedy merging of the smallest pair fails when only adjacent merges are allowed, then define and fill the DP correctly.

What the Interviewer Expects
  • Show why the Huffman-style greedy is wrong under the adjacency constraint
  • Define dp[i][j] as the min cost to merge segments i..j into one
  • Use prefix sums for O(1) range totals
  • Fill by increasing interval length
  • State O(n^3) time and mention Knuth's optimization
Key Topics to Cover
interval DP
prefix sums
dynamic programming
optimization
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
  • How does Knuth's optimization reduce this to O(n^2) and why does it apply?
  • What if each merge combines exactly K adjacent segments?
  • What if any two segments (not just adjacent) can be merged?
  • How would you reconstruct the merge order?
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: sizes of n segments in order. Each operation merges two neighbors into one segment of their summed size, costing that sum. Output: minimum total cost to reach one segment....

High-Level Design

Whatever order we choose, the last merge joins a left block i..m and a right block m+1..j, and that final merge always costs sum(i..j). So:

$dp[i][j] = \min_{i \le m < j}(dp[i][m] + dp[m+1][j])...


Submit Your Answer
Markdown supported

Related Questions