Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
A ramp in an integer array nums is a pair (i, j) for which i < j and nums[i] <= nums[j]. The width of such a ramp is j - i.
Given an integer array nums, return the maximum width of a ramp in nums. If there is no ramp in nums, return 0.
Example 1:
Input: nums = [6,0,8,2,1,5] Output: 4 Explanation: The maximum width ramp is achieved at (i, j) = (1, 5): nums[1] = 0 and nums[5] = 5.
Example 2:
Input: nums = [9,8,1,0,1,9,4,0,4,1] Output: 7 Explanation: The maximum width ramp is achieved at (i, j) = (2, 9): nums[2] = 1 and nums[9] = 1.
Constraints:
2 <= nums.length <= 5 * 1040 <= nums[i] <= 5 * 104Problem summary: A ramp in an integer array nums is a pair (i, j) for which i < j and nums[i] <= nums[j]. The width of such a ramp is j - i. Given an integer array nums, return the maximum width of a ramp in nums. If there is no ramp in nums, return 0.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Two Pointers · Stack
[6,0,8,2,1,5]
[9,8,1,0,1,9,4,0,4,1]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #962: Maximum Width Ramp
class Solution {
public int maxWidthRamp(int[] nums) {
int n = nums.length;
Deque<Integer> stk = new ArrayDeque<>();
for (int i = 0; i < n; ++i) {
if (stk.isEmpty() || nums[stk.peek()] > nums[i]) {
stk.push(i);
}
}
int ans = 0;
for (int i = n - 1; i >= 0; --i) {
while (!stk.isEmpty() && nums[stk.peek()] <= nums[i]) {
ans = Math.max(ans, i - stk.pop());
}
if (stk.isEmpty()) {
break;
}
}
return ans;
}
}
// Accepted solution for LeetCode #962: Maximum Width Ramp
func maxWidthRamp(nums []int) int {
n := len(nums)
stk := []int{}
for i, v := range nums {
if len(stk) == 0 || nums[stk[len(stk)-1]] > v {
stk = append(stk, i)
}
}
ans := 0
for i := n - 1; i >= 0; i-- {
for len(stk) > 0 && nums[stk[len(stk)-1]] <= nums[i] {
ans = max(ans, i-stk[len(stk)-1])
stk = stk[:len(stk)-1]
}
if len(stk) == 0 {
break
}
}
return ans
}
# Accepted solution for LeetCode #962: Maximum Width Ramp
class Solution:
def maxWidthRamp(self, nums: List[int]) -> int:
stk = []
for i, v in enumerate(nums):
if not stk or nums[stk[-1]] > v:
stk.append(i)
ans = 0
for i in range(len(nums) - 1, -1, -1):
while stk and nums[stk[-1]] <= nums[i]:
ans = max(ans, i - stk.pop())
if not stk:
break
return ans
// Accepted solution for LeetCode #962: Maximum Width Ramp
struct Solution;
impl Solution {
fn max_width_ramp(a: Vec<i32>) -> i32 {
let mut stack: Vec<usize> = vec![];
let n = a.len();
for i in 0..n {
if let Some(&j) = stack.last() {
if a[i] < a[j] {
stack.push(i);
}
} else {
stack.push(i);
}
}
let mut res = 0;
for i in (0..n).rev() {
while let Some(&j) = stack.last() {
if a[j] <= a[i] {
res = res.max(i - j);
stack.pop();
} else {
break;
}
}
}
res as i32
}
}
#[test]
fn test() {
let a = vec![6, 0, 8, 2, 1, 5];
let res = 4;
assert_eq!(Solution::max_width_ramp(a), res);
let a = vec![9, 8, 1, 0, 1, 9, 4, 0, 4, 1];
let res = 7;
assert_eq!(Solution::max_width_ramp(a), res);
}
// Accepted solution for LeetCode #962: Maximum Width Ramp
function maxWidthRamp(nums: number[]): number {
let [ans, n] = [0, nums.length];
const stk: number[] = [];
for (let i = 0; i < n - 1; i++) {
if (stk.length === 0 || nums[stk.at(-1)!] > nums[i]) {
stk.push(i);
}
}
for (let i = n - 1; i >= 0; i--) {
while (stk.length && nums[stk.at(-1)!] <= nums[i]) {
ans = Math.max(ans, i - stk.pop()!);
}
if (stk.length === 0) break;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair of elements. The outer loop picks one element, the inner loop scans the rest. For n elements that is n × (n−1)/2 comparisons = O(n²). No extra memory — just two loop variables.
Each pointer traverses the array at most once. With two pointers moving inward (or both moving right), the total number of steps is bounded by n. Each comparison is O(1), giving O(n) overall. No auxiliary data structures are needed — just two index variables.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Advancing both pointers shrinks the search space too aggressively and skips candidates.
Usually fails on: A valid pair can be skipped when only one side should move.
Fix: Move exactly one pointer per decision branch based on invariant.
Wrong move: Pushing without popping stale elements invalidates next-greater/next-smaller logic.
Usually fails on: Indices point to blocked elements and outputs shift.
Fix: Pop while invariant is violated before pushing current element.