Code Room
CodingHardcod-g939
Subject Monotonic stackLevel Senior–Staff~35 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Given an integer array, the range of a contiguous subarray is its maximum element minus its minimum element. Return the sum of the ranges over all contiguous subarrays. The array has at most 1000 elements; values may be negative. The answer fits in a 64-bit signed integer.

Implement
sum_subarray_ranges(arr: list[int]) → int
Examples
in[[1,2,3]]out4
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.