Sliding Window: Target Containment and Most-Repeated Window
Learn this problemProblem statement
Source note, June 26, 2026: Please ignore this question for now, as it may have source issues. If you still want to practice it, treat it as a rough draft rather than a reliable source-backed problem. I will investigate and clean it up later, but this may take a while. Sorry for the inconvenience, and thank you so much for your understanding. You are the best! 🐿️
You are given an array of integers nums, a window size k, and a target value. This MLE onsite coding round comes in two parts that share the same length-k sliding window.
Part 1 (warm-up). Return the start indices of every length-k contiguous window of nums that contains target at least once, in increasing order. Maintain a running count of how many times target appears in the current window: as the window slides one step, add the entering element, drop the leaving element, and record the start index whenever the count is non-zero. This is O(n) time and O(1) extra space.
Part 2 (the main ask). Return the start index of the length-k window in which target appears the most times. If two windows are tied for the highest count, return the smaller start index. If the array is shorter than k (no valid window exists), return -1.
Reuse the running-count slide from Part 1. Track the best start and best count separately, and only update the best when the current count is strictly greater than the recorded best, which keeps the earliest start on ties. A prefix-count array also gives O(n) time but allocates an extra O(n) array and invites off-by-one errors; the running-count slide is simpler and uses O(1) extra space.
The starter contract below is Part 2 (return the single best start index). Clarify with the interviewer whether they want every qualifying window (Part 1) or only the single best one (Part 2) before coding.
Edge cases: when k > nums.length there is no valid window, so Part 2 returns -1; when target never appears, every window ties at count 0, so Part 2 returns 0; when every element equals target, all windows tie at count k, so Part 2 returns 0.
Function
bestWindowStart(nums: int[], k: int, target: int) → intExamples
Example 1
nums = [3, 1, 3, 2, 3, 3, 4, 3]k = 4target = 3return = 2Example 2
nums = [3, 1, 1, 3, 1, 1]k = 3target = 3return = 0Example 3
nums = [1, 2, 4, 5]k = 2target = 3return = 0Example 4
nums = [1, 3]k = 5target = 3return = -1Constraints
1 <= nums.length1 <= k- If
k > nums.length, no valid window exists; return-1. - Ties for the highest target count are broken by returning the smallest start index.
- Values in
numsandtargetare 32-bit integers.
More Roblox problems
- Most Frequent Call Stack Per ThreadPHONE SCREEN · Seen Jul 2026
- Maximum Number of Balls in a BoxPHONE SCREEN · Seen Jun 2026
- Meeting Rooms IIPHONE SCREEN · Seen Jun 2026
- Most Frequent Call Path From Function Trace LogsPHONE SCREEN · Seen Jun 2026
- Single-Threaded CPUPHONE SCREEN · Seen Jun 2026
- Topological Sort with Secondary OrderingPHONE SCREEN · Seen Jun 2026
- Merge IntervalsPHONE SCREEN · Seen May 2026
- Implement a Rate LimiterPHONE SCREEN · Seen May 2026