Longest Price-Stable Stretch in an Item's Sold History
Last updated: November 29, 2026
Quick Overview
Given the sold prices of one catalog product in time order and a tolerance k, return the length of the longest contiguous stretch where the highest and lowest price differ by at most k.
eBay
November 29, 20260
0
0 solved
Given the sold prices of one catalog product in time order and a tolerance k, return the length of the longest contiguous stretch where the highest and lowest price differ by at most k.
eBay shows buyers price trends for a product from completed sales, and a stable stretch is a signal that a market price exists. The interviewer expects a sliding window and checks whether you can keep the window's max and min in O(1) amortized time with monotonic deques rather than rescanning.
What the Interviewer Expects
- Recognize a variable-size sliding window
- Maintain window max and min with two monotonic deques
- Shrink from the left only while the spread exceeds k
- Reach O(n) time with an amortized argument
- Handle k = 0 and a single sale
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 start and end indices of the stretch, not just its length.
- How would you answer this for many different k values at once?
- What changes if prices arrive as an unbounded stream and you only keep the last hour?
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: prices, a list of integers in sale order, and k >= 0. Output: the longest length of a contiguous subarray with max - min <= k. An empty list returns 0....
High-Level Design
A window [left, right] is valid if its spread is at most k, and shrinking a valid window keeps it valid, so the two-pointer pattern applies: extend right every step, advance left while invalid. ...