Grokking 75: Top Coding Interview Questions
Vote

0% completed

Minimum Window Sort (medium)

Problem Statement

Given an array, find the length of the smallest subarray in it which when sorted will sort the whole array.

Example 1:

Input: [1, 2, 5, 3, 7, 10, 9, 12]
Output: 5
Explanation: We need to sort only the subarray [5, 3, 7, 10, 9] to make the whole array sorted

Example 2:

Input: [1, 3, 2, 0, -1, 7, 10]
Output: 5
Explanation: We need to sort only the subarray [1, 3, 2, 0, -1] to make the whole array sorted

Example 3:

Input: [1, 2, 3]
Output: 0
Explanation: The array is already sorted

Example 4:

Input: [3, 2, 1]
Output: 3

.....

.....

.....

Like the course? Get enrolled and start learning!
adi berkowitz

adi berkowitz

· 2 years ago

I have comments in code explaining We basically traverse array left to right and then right to left

And we find the index that should begin the sort at each traversal

import math class Solution: def sort(self, arr): left_pointer = 0 right_pointer = len(arr)-1 index = 0 # Concept # We should traverse left to right tracking the number (left_pointer) # That needs to change while index< len(arr)-1 and left_pointer>=0: # left_pointer follows index until we have to set it back one # (take [1, 2, 5, 3, 7, 10, 9, 12] example) -> Index and left_pointer # Will keep increasing until index/left_ponter = 2 (5). Next index is 3 # This means that 5 onward needs to be sorted (for now) # So we move left_pointer back. # Now
Isabela Vlls

Isabela Vlls

· 2 years ago

class Solution: def sort(self, arr): sortedVersion = sorted(arr) left, right = 0, len(arr)-1 while left < len(arr) and arr[left] == sortedVersion[left]: left += 1 while right >= 0 and arr[right] == sortedVersion[right]: right -= 1 if left >= right: # if it's already sorted return 0 count = right - left + 1 return count
Mohammed Dh Abbas

Mohammed Dh Abbas

· 2 years ago

import math class Solution: def sort(self, arr): i = j = 0 # find the first out order position element from [left to right] --> for count in range(len(arr)): if count - 1 >= 0 and arr[count] < arr[count - 1]: i = count - 1 break # find the first out order position element from [right to left] <-- end = i - 1 if i >= 0 else 0 for count in range(len(arr) - 1, end, -1): if count + 1 < len(arr) and arr[count] > arr[count + 1]: j = count + 1 break # for example i and j are in the positions below # [-4, -2, 1, 3, 2, 9, 0, -3, 7, 10, 30] # i j # find the min and max elements between i and j inclusive # min = -3, max = 9 == the range is out of order between [-
Abdullah AlKheshen

Abdullah AlKheshen

· 2 years ago

Compare 7 and -1, stop. high = 5.

The above line is wrong because as long as arr[hi] >= arr[hi-1] we decrement hi--;

7 >= -1 so hi should stop on -1 that is index 4 not at 7 with index 5

Fei Chen

Fei Chen

· 2 years ago

import math class Solution: def sort(self, arr): sorted_arr = sorted(arr) if sorted_arr == arr or len(arr) == 1: return 0 l, r = 0, len(arr) - 1 while l <= r: if arr[l] == sorted_arr[l]: l+= 1 elif arr[r] == sorted_arr[r]: r -= 1 else: break return r-l+1
Faraz Ahmed

Faraz Ahmed

· 3 years ago

i have dry run it multiple times, iam getting 6 as the size of subarray window? i think visualization of algorithm is misleading , if you dry run the first input based on the visualization of algorithm you get 6 as the size of subarray!

i might be wrong, but i have dry run it twice, what you guys think???

i think the problem is here:     while (low > 0 && arr[low - 1] > subarrayMin) {       low -= 1;     }     // extend the subarray to include any number which is smaller than the maximum of     // the subarray     while (high < arr.length - 1 && arr[high + 1] < subarrayMax) {       high += 1;     }

While in visualization it compares arr[min] > subarray_min ? then low-- , to insert from beginning

the min and max i found in subarray 👇 min=3 max=10

the first inorder from beginning

Show 1 reply
R

rvadhith

· 3 years ago

Actual answer is 0. Submission is expecting 1.

Dang Nguyen

Dang Nguyen

· 3 years ago

[22, 34, 59, 60, 75, 76, 81] fails because it expects 1 during submission, but when I run it before submission it expects 0.

Deshapriya

Deshapriya

· 3 years ago

public static int sort(int[] arr) {

int min = Integer.MAX_VALUE;

    int max = Integer.MIN_VALUE;

    if (arr.length == 1) return 0; 

    for (int i = 0; i < arr.length; ++i) {

        if (i == 0) {

            if (arr[i] > arr[i + 1]) {

                min = Math.min(min, arr[i]);

                max = Math.max(max, arr[i]);

            }

        } else if (i == arr.length - 1) {

            if (arr[i] < arr[i - 1]) {

                min = Math.min(min, arr[i]);

                max = Math.max(max, arr[i]);

            }

        } else {

            if (arr[i] > arr[i + 1] || arr[i] < arr[i - 1]) {

                min = Math.min(min, arr[i]);

                max = Math.max(max, arr[i]);

         
M

Mikhail Putilov

· 3 years ago

Why is it medium? It is impossible to come up with a solution O(n) in a reasonable time...

Reading Progress

0%


Vote for new content