Longest Trading Window With Bounded Price Spread

Last updated: November 29, 2026

Quick Overview

Given a list of trade prices in time order and a tolerance k, return the length of the longest contiguous window whose max price minus min price is at most k.

Voleon
Coding & Algorithms
Software Engineer
Voleon
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a list of trade prices in time order and a tolerance k, return the length of the longest contiguous window whose max price minus min price is at most k.

Voleon's coding screens favor clean Python on problems that look like real market-data chores, such as finding calm regimes in a price series. The interviewer is checking whether you move past the O(n^2) rescan to a sliding window with monotonic deques and can argue why each index enters and leaves once.

What the Interviewer Expects
  • Clarify inclusive spread, empty input and whether k can be zero
  • Use a sliding window whose left edge only moves forward
  • Maintain running max and min with two monotonic deques
  • Achieve O(n) time with an amortized argument
  • Test duplicates and strictly alternating prices
Key Topics to Cover
sliding window
monotonic deque
amortized analysis
time series
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
  • How would you return the window boundaries as well as its length?
  • What if the spread tolerance is a percentage of the window's minimum price?
  • How would you adapt this to an unbounded stream with a maximum window size?
  • Could you answer many queries with different k values efficiently?
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: prices (floats in time order) and k >= 0. Output: the length of the longest contiguous subarray with max - min <= k. Empty input returns 0. Equal prices are allowed and count toward the w...

High-Level Design

If a window violates the spread, any larger window containing it also violates it, so the valid windows are monotone in the left edge. That justifies a two-pointer scan: extend right every step and ...


Submit Your Answer
Markdown supported