← All questions
MediumCoding
Shortest Subarray to be Removed to Make Array Sorted
Asked at
Goldman Sachs
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below
0
Stuck? Show a way to structure it+
- 01Find the longest nondecreasing prefix and suffix.
- 02If they overlap, return zero.
- 03Initially remove either middle complement.
- 04Merge prefix and suffix with two pointers to minimize the removed gap.
Reference answer
Then expect these follow-ups
Why may the kept array consist of two runs?
Tests: invariant reasoning
How would strict increasing order change comparisons?
Tests: edge cases
Free to read · better with Enzo
Practice this out loud with Enzo
Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.
Next question