Minimum Cost To Consolidate Adjacent Sub-Accounts K At A Time

Last updated: November 29, 2026

Quick Overview

A pod's capital sits in n adjacent sub-accounts with given balances. One operation merges exactly k consecutive sub-accounts into one, costing the sum of their balances. Return the minimum total cost to end with a single account, or -1 if impossible.

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

0

0

0 solved


A pod's capital sits in n adjacent sub-accounts with given balances. One operation merges exactly k consecutive sub-accounts into one, costing the sum of their balances. Return the minimum total cost to end with a single account, or -1 if impossible.

This is an interval DP problem in a fund-operations wrapper, the kind of hard onsite question Point72 uses to separate candidates who can define a state space from those who memorize patterns. The interviewer looks for the feasibility check, a correct dp over subarrays, and an honest complexity bound.

What the Interviewer Expects
  • Check feasibility: (n - 1) must be divisible by (k - 1)
  • Define dp[i][j] as the min cost to reduce the subarray to as few piles as possible
  • Split only at positions that keep the left part reducible to one pile (step k - 1)
  • Add the subarray sum when the segment can collapse to a single pile
  • Use prefix sums for range sums
  • State O(n^3 / k) time and O(n^2) space
Key Topics to Cover
interval DP
prefix sums
feasibility invariants
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
  • Why does stepping the split point by k - 1 preserve correctness?
  • How would you reconstruct the sequence of merges?
  • If k = 2, is there a faster algorithm and why does Huffman coding not apply?
  • What if accounts were not required to be adjacent?
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

Merges must take exactly k adjacent accounts, and each merge costs the total balance of the merged accounts. We want the cheapest sequence ending with one account. Every merge reduces the count by k -...

High-Level Design

Interval DP. dp[i][j] is the minimum cost to reduce balances[i..j] to the fewest piles possible, which is 1 + ((len - 1) mod (k - 1)). To compute it, split into a left part i..m that collapses to exac...


Submit Your Answer
Markdown supported

Related Questions