The Maximum Score of a Good Subarray interview question is a high-level array problem that combines range optimization with a specific constraint. You are given an array nums and an index k. A "good" subarray is a contiguous subarray nums[i...j] such that i <= k <= j. The score of a subarray is defined as min(nums[i...j]) * (j - i + 1).
The goal is to find the maximum possible score among all "good" subarrays.
Google asks the Maximum Score of a Good Subarray coding problem to evaluate a candidate's ability to optimize search using Two Pointers or Monotonic Stacks. It tests whether you can recognize that as you expand the subarray from the starting point k, the minimum value only decreases or stays the same. It evaluates your ability to make locally optimal expansion decisions to find the global maximum score.
The Array, Monotonic Stack, Two Pointers, Binary Search, Stack interview pattern applies. The most intuitive and efficient approach is the Two-Pointer method:
left = k and right = k. Initialize min_val = nums[k].left or right.nums[left-1] and nums[right+1]. This keeps the min_val of the new window as high as possible.min_val and calculate score = min_val * (right - left + 1).nums: [1, 4, 3, 7, 4, 5], k = 3.
A common mistake in the Maximum Score of a Good Subarray coding problem is using a brute-force approach, checking all pairs . Another error is not correctly handling the greedy expansion—perhaps always moving one pointer first or moving based on a wrong condition. Candidates also sometimes forget that the minimum value must be updated every time the window expands.
When you need to expand from a specific index while maintaining a property (like a minimum or maximum), Two Pointers with a greedy expansion is often the best approach. Practice this "expanding window" technique as it's a common variation of the standard sliding window.