Sum the Minimum Stock Level Over Every Contiguous Day Range

Last updated: November 29, 2026

Quick Overview

Given daily inventory levels for a SKU at a fulfillment center, compute the sum over all contiguous day ranges of the minimum level in that range. Return the result modulo 10^9 + 7.

Amazon
Coding & Algorithms
Software Engineer
Amazon
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given daily inventory levels for a SKU at a fulfillment center, compute the sum over all contiguous day ranges of the minimum level in that range. Return the result modulo 10^9 + 7.

An Amazon onsite contribution-counting question. The interviewer wants you to flip the sum: count how many ranges each day is the minimum of, using previous-smaller and next-smaller boundaries from monotonic stacks, with a careful tie rule.

What the Interviewer Expects
  • Reframes as each element's contribution
  • Finds previous smaller and next smaller boundaries with stacks
  • Uses strict on one side and non-strict on the other to avoid double counting ties
  • Multiplies left span by right span by value
  • Applies the modulus
Key Topics to Cover
monotonic stack
contribution technique
combinatorics
tie breaking
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Sum of range maximums minus range minimums.
  • Only ranges of length at most L.
  • How would you answer this for a sliding 30-day window?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list of positive integers, length up to 3 * 10^4. Output: sum of min(arr[i..j]) over all i <= j, mod 10^9 + 7. O(n^2) enumeration is too slow at the top of the range....

High-Level Design

arr[i] is the minimum of every range that starts after the previous smaller element and ends before the next smaller element. Let left[i] be the number of choices for the start (i minus the index of t...


Submit Your Answer
Markdown supported