Shortest Middle Segment to Delete So Release Versions Become Sorted
Last updated: November 29, 2026
Quick Overview
A list of build version numbers should be non-decreasing, but some entries in the middle are out of order. Return the length of the shortest contiguous segment you can delete so the remaining list is non-decreasing.
0
0
0 solved
A list of build version numbers should be non-decreasing, but some entries in the middle are out of order. Return the length of the shortest contiguous segment you can delete so the remaining list is non-decreasing.
A Google onsite two-pointer question where a sorted prefix and a sorted suffix must be stitched together. The interviewer checks that the candidate identifies both monotone ends first and then merges them with two pointers instead of trying every cut.
What the Interviewer Expects
- Finds the longest sorted prefix and sorted suffix
- Considers deleting everything after the prefix or before the suffix
- Uses two pointers to join a prefix element with a suffix element
- Handles an already sorted list
- States O(n) time
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What if you may delete the segment and must also keep at least one element?
- What if you may delete any set of elements, not only a contiguous one?
- How would you return the segment boundaries?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list
vof integers. - Delete one contiguous segment (possibly empty) so the rest is non-decreasing.
- Output: the minimum length of that segment....
High-Level Design
Let right be the start of the longest non-decreasing suffix. If right <= 0, the list is sorted (or empty), answer 0. Otherwise one option is to delete v[0:right], cost right.
Now extend a sor...