Shortest Unsorted Continuous Subarray (Array/Pointers)

Data Structures
Medium
Google
55.8K views

Given an integer array, you need to find one continuous subarray that, if you only sort this subarray, the whole array will be sorted. Find its length.

Why Interviewers Ask This

Google interviewers ask this to assess your ability to optimize beyond brute force. They evaluate if you can identify edge cases where sorting the entire array is inefficient. The problem tests your mastery of two-pointer techniques and your skill in analyzing time complexity, specifically distinguishing between O(n log n) sorting approaches versus optimal O(n) linear scans.

How to Answer This Question

1. Clarify the goal: Confirm that sorting only a specific subarray must result in the entire array being sorted. 2. Analyze constraints: Acknowledge the need for an O(n) solution rather than naive O(n log n) sorting. 3. Identify boundaries: Explain how to find the start index by scanning left-to-right to find the first element out of order relative to the maximum seen so far. 4. Define end points: Describe scanning right-to-left to locate the last element smaller than the minimum seen from the right. 5. Calculate length: Subtract the indices to get the final answer. Throughout, explicitly mention handling edge cases like already sorted arrays or single-element mismatches to demonstrate thoroughness.

Key Points to Cover

  • Demonstrating awareness of O(n) vs O(n log n) complexity trade-offs
  • Using two-pass logic to define exact boundary indices without sorting
  • Handling edge cases like pre-sorted arrays or duplicate values
  • Clearly articulating the relationship between local disorder and global order
  • Maintaining constant space complexity while achieving linear time

Sample Answer

To solve this efficiently, I would avoid full sorting since it takes O(n log n). Instead, I aim for an O(n) approach using two passes. First, I'll traverse from left to right, tracking the maximum value encountered. If I find an element smaller than this current maximum, it means this element is out of place and marks a potential end of our unsorted subarray. I update my 'end' pointer whenever this happens. Next, I'll do a reverse pass from right to left, tracking the minimum value. If an element is larger than this running minimum, it indicates an out-of-order position on the left side, updating my 'start' pointer. Finally, if no such elements are found, the array is already sorted. Otherwise, the length is simply end minus start plus one. For example, in [2, 6, 4, 8, 10, 9, 15], the max scan identifies 4 as out of place after 6, setting end to index 2. The min scan finds 6 out of place after 10, setting start to index 1. Sorting indices 1 through 5 fixes the whole array. This method ensures we only touch necessary elements, aligning with Google's focus on performance optimization.

Common Mistakes to Avoid

  • Immediately suggesting full array sorting which misses the O(n) requirement
  • Failing to handle the case where the array is already perfectly sorted
  • Confusing the direction of comparisons when tracking min/max values
  • Not accounting for duplicate numbers which can skew boundary detection

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 87 Google questions