Get Stable Periods Count
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Amazon's "Get Stable Periods Count" hit the assessment circuit in December 2024, and it's a sliding window problem dressed up as a data analysis question. You've got an array and you need to count contiguous subarrays where some stability condition holds. The trick is knowing when to expand and contract your window, not iterating through every possible subarray. StealthCoder can feed you the window bounds and the count logic in real time if you freeze on the exact condition.
Pattern and pitfall
This is a classic sliding window play. You maintain a left and right pointer, expand right to include new elements, and shrink left when the stability condition breaks. The condition itself is usually something like "max minus min in the window is at most K" or "all elements are within a range". The naive approach of checking every subarray pair is O(n²) and times out. The two-pointer approach is O(n) because each element enters and exits the window once. Common mistake: forgetting to count all valid subarrays when you shrink left, not just the window itself. If you blank on the exact stability rule during the OA, StealthCoder gives you the condition and the window logic.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Get Stable Periods Count 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 for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as count number of nice subarrays. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Get Stable Periods Count FAQ
What's the stability condition in this problem?+
Without the full problem text, it's likely that a period is stable if the difference between max and min elements in that window is at most some threshold, or all elements fall within a range. Amazon problems usually hide the condition in the problem statement, so read carefully for inequality or difference constraints.
Is this really just sliding window?+
Yes. Two pointers, expand right, contract left when the condition fails. Count all valid subarrays ending at each right position. It's O(n) time, O(1) or O(k) space depending on whether you track element frequencies. Much faster than brute force.
How do I count subarrays correctly?+
When your window is valid at position right, every subarray from left to right (and all positions between) is valid. That's (right - left + 1) subarrays. Add that to your total each iteration. Don't just count the full window.
Should I use a hashmap or array to track elements?+
Use a frequency map or array depending on the element range. If elements are integers in a small range, an array is faster. If they're arbitrary, use a hashmap. You need to track counts to know when the window is stable, so you'll update it as you slide.
How much time do I really have to solve this?+
Amazon OAs typically give 45-60 minutes per problem. Sliding window is implementable in 15-20 minutes once you spot the pattern. If you haven't identified the condition and window logic by minute 10, re-read the problem or outline the brute force first.