Minimum Cost to Split a Delivery Route at Required Hubs

Last updated: November 29, 2026

Quick Overview

A linehaul route of length L must be split at a given set of hub positions. Splitting a segment costs its current length, and splits can be done in any order; return the minimum total cost to make all splits.

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

0

0

0 solved


A linehaul route of length L must be split at a given set of hub positions. Splitting a segment costs its current length, and splits can be done in any order; return the minimum total cost to make all splits.

A harder interval DP question that appears in Flipkart onsite loops for SDE-2 roles, wrapped in Ekart linehaul planning language. The interviewer is checking whether you see that order matters, define state over pairs of cut points, and get the O(n^3) recurrence right.

What the Interviewer Expects
  • Show with an example that the order of splits changes total cost
  • Add the route endpoints 0 and L to the sorted cut list
  • Define dp[i][j] as min cost to make all cuts strictly between cut i and cut j
  • Fill the table by increasing interval length
  • State O(m^3) time and O(m^2) space for m cuts
Key Topics to Cover
interval DP
2D dynamic programming
memoization
optimal substructure
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
  • Can you reconstruct the actual order of splits?
  • Is there a faster algorithm, and when does Knuth's optimization apply?
  • How does this relate to building an optimal binary search tree?
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: route length L and up to about 100 hub positions strictly inside (0, L).
  • Each split costs the length of the piece being split at that moment.
  • Output: min total cost over all split ord...
High-Level Design

Order matters: for L = 7, cuts [1, 3, 4, 5] in that order cost 7 + 6 + 4 + 3 = 20, but a better order costs 16. Brute force over orders is factorial. Sort cuts and add sentinels 0 and L, giving po...


Submit Your Answer
Markdown supported