Partition Array into Disjoint Intervals

MediumArrays & Hashingprefix sum

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Split the integer array nums into a non-empty prefix left and a non-empty suffix right such that every element of left is less than or equal to every element of right. Among all valid splits, choose the one where left is as short as possible, and return the length of left. The input is guaranteed to have at least one valid split.

Examples

Input: nums = [5,0,3,8,6]
Output: 3
left = [5,0,3] and right = [8,6]; max(left) = 5 <= min(right) = 6.
Input: nums = [1,1,1,0,6,12]
Output: 4
left must include the 0, so left = [1,1,1,0] and right = [6,12].

Constraints

  • `2 <= nums.length <= 10^5`
  • `0 <= nums[i] <= 10^6`
  • At least one valid split exists

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More prefix sum problems