Skip to content
Hello Python

Shortest Subarray to Remove for Sorted Order

Checking your account…

Sign in to save your code and progress across devices. The lesson and problem statement remain public.

Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace

Problem

Implement Solution.findLengthOfShortestSubarray(arr). Remove one contiguous subarray, possibly empty, so the remaining values are nondecreasing. Return the minimum removed length.

Starter code

class Solution:
    def findLengthOfShortestSubarray(self, arr):
        pass
Test cases

middle-removal

{
  "args": [
    [
      1,
      2,
      3,
      10,
      4,
      2,
      3,
      5
    ]
  ]
}

Expected: 3

Wizard outline
  1. Step 1: Initialize Solution.findLengthOfShortestSubarray

    Replace the empty starter with the first real state owned by Solution.findLengthOfShortestSubarray. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.

  2. Step 2: Assemble the primary transition

    Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.

  3. Step 3: Pass the Descending case

    Complete the readable core algorithm for one representative Interview case. Find maximal sorted prefix/suffix regions and use two pointers to find their closest compatible join.

  4. Step 4: Harden the Middle Removal boundary

    Repair the reviewed boundary and pass the complete submission contract. Any kept result consists of a prefix and suffix that are individually sorted. The scan considers every possible endpoint in the maximal sorted prefix and chooses the earliest compatible suffix start, minimizing the gap removed for that endpoint and therefore globally.

Footguns and prerequisites
  • Nondecreasing order allows equal adjacent values; comparisons must use <=.
  • arrays strings two pointers sliding window
Reviewed references
Practice prerequisites
  • Discard Pairs with Two Pointers(opens in a new tab)

    Discard Pairs with Two Pointers isolates every pair outside [left, right] has already been counted or proven too large, and no unresolved pair is skipped. That focused state discipline is required when implementing shortest subarray remove sorted as a complete Interview Problem.

Recommended approach and implementation

Locate the earliest nondecreasing suffix. Initially remove either the prefix before it or the suffix after the maximal sorted prefix, then scan prefix indices and advance the suffix pointer until the boundary values can join.

Why it works: Any kept result consists of a prefix and suffix that are individually sorted. The scan considers every possible endpoint in the maximal sorted prefix and chooses the earliest compatible suffix start, minimizing the gap removed for that endpoint and therefore globally.

class Solution:
    def findLengthOfShortestSubarray(self, arr):
        """
        Checkpoint 1: initialize the state owned by this Interview contract.
        Checkpoint 2: assemble the primary transition without hiding the boundary.
        """
        n = len(arr)
        suffix = n - 1
        while suffix > 0 and arr[suffix - 1] <= arr[suffix]:
            suffix -= 1
        if suffix == 0:
            return 0
        best = suffix
        prefix = 0
        while prefix + 1 < n and arr[prefix] <= arr[prefix + 1]:
            while suffix < n and arr[prefix] > arr[suffix]:
                suffix += 1
            best = min(best, suffix - prefix - 1)
            prefix += 1
        return min(best, n - prefix - 1)