Largest Price Swing Within Any K-Tick Window on Yahoo Finance
Last updated: November 29, 2026
Quick Overview
Given a list of trade prices for a ticker and an integer k, return the largest difference between the maximum and minimum price inside any window of k consecutive ticks. Solve it in linear time.
Yahoo
November 29, 20260
0
0 solved
Given a list of trade prices for a ticker and an integer k, return the largest difference between the maximum and minimum price inside any window of k consecutive ticks. Solve it in linear time.
Yahoo Finance shows intraday ranges and volatility on quote pages, so streaming window statistics are a natural coding topic. The interviewer wants to see you move from an O(nk) scan to monotonic deques and reason about why each index is pushed and popped once.
What the Interviewer Expects
- Start with the brute force and state its O(nk) cost
- Maintain a decreasing deque for maxima and an increasing deque for minima
- Evict indices that fall out of the window from the front
- Explain the amortized O(n) argument
- Handle k larger than the list and k equal to 1
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
- How would you report the window start index along with the swing?
- How would you adapt this to a time-based window (last 5 minutes) with irregular ticks?
- Can you do this for a live stream with bounded memory?
- What if you needed the swing for every window, not just the maximum?
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 (list of floats or ints) and k. Output: max over windows of (max(window) - min(window)). If k >= len(prices) treat the whole list as one window; an empty list returns 0....
High-Level Design
Brute force recomputes max and min for each window: O(nk). Instead keep two deques of indices:
hiholds indices with strictly decreasing prices, sohi[0]is the window max.loholds indices ...