Minimum Cost to Merge Adjacent Sorted Db2 Partition Files

Last updated: November 29, 2026

Quick Overview

Given the sizes of sorted partition files laid out in order, you may repeatedly merge two adjacent files at a cost equal to their combined size; return the minimum total cost to produce one file.

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

0

0

0 solved


Given the sizes of sorted partition files laid out in order, you may repeatedly merge two adjacent files at a cost equal to their combined size; return the minimum total cost to produce one file.

Db2 and IBM's data warehousing engines merge sorted runs during compaction and reorgs, and when only adjacent runs may be merged the greedy Huffman answer breaks. This interval DP question shows up in harder IBM onsite rounds to see whether you can spot why greedy fails and define a correct state.

What the Interviewer Expects
  • Explain why the Huffman greedy does not apply when only adjacent merges are allowed
  • Define dp over intervals with a split point
  • Use prefix sums to get interval cost in O(1)
  • Fill the table by increasing interval length
  • State O(n^3) time and mention Knuth's optimization
Key Topics to Cover
interval DP
prefix sums
optimal substructure
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
  • If any two files could be merged, what is the optimal algorithm?
  • How would you reconstruct the merge order?
  • How does Knuth's optimization reduce this to O(n^2) and why does it apply?
  • What if each merge combines exactly k adjacent files?
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 sizes of positive integers. Output: minimal total merge cost. A single file costs 0. Example: [1,2,3] best merges 1 and 2 (cost 3) then 3 and 3 (cost 6), total 9, versus 11 the other w...

High-Level Design

The last merge of interval [i, j] joins some left block [i, m] with a right block [m+1, j], and costs sum(i..j) no matter where the split is. So `dp[i][j] = min over m of dp[i][m] + dp[m+1][j]...


Submit Your Answer
Markdown supported

Related Questions