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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

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
Two Pointers
Sorted Prefix and Suffix
Arrays
Merging
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
  • 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 Problems
Sample Answer
Requirements
  • Input: list v of 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...


Submit Your Answer
Markdown supported