Sliding Window Centered Monotonicity - Pinterest OA
Question Description
You are given an integer array nums and a radius k. The task is to return all indices i such that the centered window [i-k, ..., i, ..., i+k] is fully inside the array and values strictly decrease outward from the center on both sides.
Core idea: for each candidate center i you must ensure i-k >= 0 and i+k < n, and for every 1 <= t <= k the left side is strictly increasing into the center (nums[i-t] < nums[i-t+1]) while the right side is strictly decreasing away from the center (nums[i+t] < nums[i+t-1]). In other words you want a strictly increasing prefix ending at i of length >= k and a strictly decreasing suffix starting at i of length >= k.
High-level flow you should explain in an interview:
- Validate trivial bounds (n, k, early return when 2*k+1 > n).
- Do a left-to-right pass to compute for each index how many consecutive increasing steps end at that index.
- Do a right-to-left pass to compute how many consecutive decreasing steps start at that index.
- Collect indices i where both counts are >= k.
Skill signals: array manipulation, sliding-window / two-pointers intuition, prefix/suffix DP (counting runs), O(n) time and O(n) space reasoning, careful handling of equality (strictness) and edge cases (k=0, small arrays). Be ready to explain complexity and trade-offs (in-place vs extra arrays), and to walk through a short example by hand.
Common Follow-up Questions
- •How would your solution change if the monotonicity were non-strict (allow equal adjacent values)?
- •Can you modify the algorithm to run in O(1) extra space (in-place) while preserving O(n) time?
- •How would you find the number of valid centers for every possible k efficiently (for many k queries)?
- •What changes if the array is circular (wrap-around windows) — how do you detect valid centered windows on a ring?
Related Questions
Explore More Questions
Practice This Question with AI
Get real-time hints, detailed requirements, and insightful analysis of the question.