Stock Price Range over a Sliding Day Window
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The detail that matters in this Bloomberg question from May 2020 is the phrase "most recent k prices." It's not a rolling window you slide across the whole array. You only need the tail. If you've got a Bloomberg OA coming, expect a short, deceptively easy problem with a few edge cases that trip people up. Empty input returns -1, and k can be bigger than the array. Nail those and the core is a few lines. If you blank under the clock, StealthCoder runs invisibly on your screen as a safety net and hands you the solution.
The problem
Return maximum minus minimum among the most recent k prices in stockPrices. If k exceeds the number of prices, use the full history. Return -1 for empty input. Function stockPriceRange(stockPrices: int[], k: int) → int Examples Example 1 stockPrices = [4,1,8,2] k = 2 return = 6 The latest prices are 8 and 2. Constraints k > 0. At most 10^5 prices.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that this isn't a real sliding window problem. You're asked for one answer, the range of the last k prices, not a range at every position. So slice the tail: start = max(0, n - k), then scan from start to the end tracking min and max in one pass. That's O(n) time and O(1) space, and it fits the 10^5 limit easily. The common pitfalls are the edge cases. Check for empty input first and return -1. Clamp k to n so you don't get a negative index. Don't sort the slice, since that adds a pointless log factor. Don't reach for a monotonic deque either, because that's overkill for a single query. In the example [4,1,8,2] with k=2, the tail is [8,2], so the answer is 6. If your mind goes blank on the index math during the live OA, StealthCoder is the hedge that gives you the clean version.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Stock Price Range over a Sliding Day Window cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Stock Price Range over a Sliding Day Window FAQ
How hard is the Bloomberg stock price range question really?+
Easy. It's a single pass over the last k elements tracking min and max. The difficulty is only in the edge cases: empty input returning -1 and k larger than the array length. Most people who miss it skip one of those two checks.
What's the trick to solving stockPriceRange?+
Realize you need one answer, not one per window position. Compute start = max(0, n - k), then loop from start to the end keeping a running min and max. Return max minus min. No deque, no sorting, no extra data structures.
Do I need a monotonic deque for this?+
No. A deque helps when you need the range at every window position. Here you only want the most recent k prices once. A deque adds complexity and bug risk for zero gain. A simple linear scan is correct and fast.
What edge cases should I test before submitting?+
Test an empty array, which returns -1. Test k greater than the length, which uses the full history. Test k equal to 1, which returns 0. Test all identical prices. Also check the given example: [4,1,8,2] with k=2 should return 6.
How do I prepare for this in 48 hours?+
Practice a few array tail and window problems until the index clamping feels automatic. Write the min and max scan from memory, then run through the edge cases. This one is short, so spend your time on clean code and not on fancy structures.