Quantitative trading models at Two Sigma and Citadel seek regime stability by identifying periods of bounded price movement. You are given an array of integer stock prices prices and an integer volatility ceiling limit.
A contiguous trading period (subarray) is said to have bounded volatility if the absolute difference between the maximum and minimum price within the period does not exceed limit:
Your Task
Implement longest_bounded_subarray(prices: list[int], limit: int) -> int that returns the maximum length of a contiguous subarray satisfying the bounded volatility condition in time.
Constraints
Examples
prices = [8, 2, 4, 7]
limit = 4
# Subarrays with max - min <= 4 include [2, 4], [4, 7], [2, 4, 7] (max=7, min=2, diff=5 > 4 invalid)
# Valid longest: [2, 4] length 2, [4, 7] length 2. Output: 2
prices = [10, 1, 2, 4, 7, 2]
limit = 5
# Subarray [2, 4, 7, 2] has max=7, min=2, diff=5 <= 5, length 4.
# Output: 4