Minimum Cost to Compact Adjacent Ledger Segments Into One

Last updated: November 29, 2026

Quick Overview

A ledger is stored as time-ordered segment files of known sizes, and compaction can only merge two adjacent segments at a cost equal to their combined size. Find the minimum total cost to compact everything into one segment and the merge plan.

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

0

0

0 solved


A ledger is stored as time-ordered segment files of known sizes, and compaction can only merge two adjacent segments at a cost equal to their combined size. Find the minimum total cost to compact everything into one segment and the merge plan.

Coinbase stores large append-only ledgers and blockchain indexes where compaction cost is real disk and IO budget. The interviewer is testing interval DP, and in particular whether you notice that the adjacency constraint rules out the Huffman-style greedy.

What the Interviewer Expects
  • Recognize why merging the two smallest overall is invalid with adjacency
  • Define cost[i][j] as the minimum cost to merge segments i through j
  • Use prefix sums so the final merge cost is O(1)
  • Fill by increasing interval length and record split points
  • Reconstruct the plan and state O(n^3) complexity
Key Topics to Cover
interval DP
prefix sums
dynamic programming
reconstruction
storage compaction
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
  • Without the adjacency constraint, what algorithm gives the optimum?
  • How would Knuth's optimization reduce this to O(n^2)?
  • What if each compaction merges exactly k adjacent segments?
  • How would you choose a compaction policy online as new segments arrive?
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 segment sizes in time order. A merge combines two adjacent segments into one whose size is the sum, at that same cost. Output: minimum total cost and an ordered list of merges described...

High-Level Design

The last merge combines some left block [i..m] and right block [m+1..j], and costs the total size of [i..j] regardless of m. So `cost[i][j] = min over m of cost[i][m] + cost[m+1][j] + sum(i..j...


Submit Your Answer
Markdown supported