Total Volatility Summed Over Every Window of Ad Bids
Last updated: November 29, 2026
Quick Overview
Given ad bids in auction order, return the sum over all contiguous windows of (maximum bid minus minimum bid) in linear time.
Meta
November 29, 20260
0
0 solved
Given ad bids in auction order, return the sum over all contiguous windows of (maximum bid minus minimum bid) in linear time.
A Meta onsite follow-up that often comes after an O(n^2) answer: the interviewer asks for linear time, framed as measuring bid volatility across all windows in an ads auction log. They are checking whether you split the sum into sum of maxima minus sum of minima and use contribution counting with consistent tie breaking.
What the Interviewer Expects
- Give the O(n^2) solution that tracks running max and min per start
- Decompose into sum of window maxima minus sum of window minima
- Count windows where each element is the max (and min) with monotonic stacks
- Break ties so each window is counted once
- Reach O(n) time
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Return the sum modulo 10^9 + 7 for large inputs.
- Only count windows of length at least L.
- Answer the same question for many query ranges.
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list b of integers (may be negative), length up to 10^5. Output: sum over all i <= j of max(b[i..j]) - min(b[i..j]). Windows of length 1 contribute 0. Exact integer result....
High-Level Design
The total is sum of window maxima - sum of window minima. For the minima, each index m is the chosen minimum of left * right windows, where left is the distance to the previous strictly smaller va...