Problem · Sliding Window

Sliding Window: Target Containment and Most-Repeated Window

Learn this problem
MediumRobloxPHONE SCREEN
See Roblox hiring insights

Problem 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) → int

Examples

Example 1

nums = [3, 1, 3, 2, 3, 3, 4, 3]k = 4target = 3return = 2
Counts of 3 per length-4 window: start 0 [3,1,3,2]=2, start 1 [1,3,2,3]=2, start 2 [3,2,3,3]=3, start 3 [2,3,3,4]=2, start 4 [3,3,4,3]=3. The maximum count is 3, first reached at start index 2.

Example 2

nums = [3, 1, 1, 3, 1, 1]k = 3target = 3return = 0
Each length-3 window contains exactly one 3, so all windows tie at count 1. The tie-break returns the earliest start, which is 0.

Example 3

nums = [1, 2, 4, 5]k = 2target = 3return = 0
The target 3 never appears, so all windows tie at count 0. The earliest start is 0.

Example 4

nums = [1, 3]k = 5target = 3return = -1
k is larger than the array length, so no length-k window exists and the answer is -1.

Constraints

  • 1 <= nums.length
  • 1 <= 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 nums and target are 32-bit integers.

More Roblox problems

drafts saved locally
public int bestWindowStart(int[] nums, int k, int target) {
  // write your code here
}
nums[3, 1, 3, 2, 3, 3, 4, 3]
k4
target3
expected2
checking account