Largest Value in Exactly One Fixed-Length Subarray

Quick Overview

Find the largest value present in exactly one length-k subarray. Distinguish window membership from repeated occurrences, with examples and a complete C++ solution.

Largest Value in Exactly One Fixed-Length Subarray

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

### Problem You are given an integer array `nums` and an integer `k`. Call a value **almost missing** if it is present in exactly one contiguous subarray of length `k`. Return the largest almost missing value from `nums`, or `-1` if there is none. Count the number of length-`k` subarrays containing a value, rather than its number of occurrences. Several copies of a value inside one subarray still count as presence in that one subarray. ```cpp class Solution { public: int largestInteger(vector<int>& nums, int k); }; ``` ### Examples **Example 1:** ```text Input: nums = [8,8,2], k = 3 Output: 8 Explanation: The only length-3 subarray is the whole array. Both 8 and 2 are present in exactly that one subarray, even though 8 occurs twice within it. The larger eligible value is 8. ``` **Example 2:** ```text Input: nums = [4,9,4,2], k = 2 Output: 2 Explanation: The subarrays are [4,9], [9,4], and [4,2]. Value 4 is present in three of them, 9 in two, and 2 in one. Only 2 is almost missing. ``` ### Constraints - `1 <= nums.length <= 50`. - `0 <= nums[i] <= 50`. - `1 <= k <= nums.length`. ### Clarifying Questions - Are the subarrays contiguous? Yes. - Can overlapping subarrays each count? Yes. Different starting indices identify different subarrays. - Does a value occurring once in the whole array necessarily qualify? No; that position may belong to several length-`k` subarrays. - What happens when `k` equals the array length? There is only one subarray, so every value appearing in the array qualifies. ### What a Strong Answer Covers - Counts distinct subarrays containing each value, without counting repeated copies within a subarray more than once. - Applies the exactly-one condition and returns the largest eligible value. - Handles overlapping subarrays, `k = 1`, `k = nums.length`, and the absence of an eligible value. - Gives bounds appropriate for the stated array length and value range. ### Follow-up Questions - For `k = 1`, how does this definition relate to the frequency of a value in the array? - Why does counting array occurrences give a different result from counting containing subarrays? ```hint Record presence separately for each subarray For each legal starting index, identify which values occur in its length-k subarray. Increment a value's subarray count once for that starting index, even if several positions have that value. ```

Overview: Find the largest value present in exactly one length-k subarray. Distinguish window membership from repeated occurrences, with examples and a complete C++ solution.

Google logo
Google
Source window: 0–3 months · as of Oct 21, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Problem

You are given an integer array nums and an integer k. Call a value almost missing if it is present in exactly one contiguous subarray of length k.

Return the largest almost missing value from nums, or -1 if there is none.

Count the number of length-k subarrays containing a value, rather than its number of occurrences. Several copies of a value inside one subarray still count as presence in that one subarray.

class Solution {
public:
    int largestInteger(vector<int>& nums, int k);
};

Examples

Example 1:

Input: nums = [8,8,2], k = 3
Output: 8
Explanation: The only length-3 subarray is the whole array. Both 8 and 2 are present in exactly that one subarray, even though 8 occurs twice within it. The larger eligible value is 8.

Example 2:

Input: nums = [4,9,4,2], k = 2
Output: 2
Explanation: The subarrays are [4,9], [9,4], and [4,2]. Value 4 is present in three of them, 9 in two, and 2 in one. Only 2 is almost missing.

Constraints

  • 1 <= nums.length <= 50 .
  • 0 <= nums[i] <= 50 .
  • 1 <= k <= nums.length .

Clarifying Questions Guidance

  • Are the subarrays contiguous? Yes.
  • Can overlapping subarrays each count? Yes. Different starting indices identify different subarrays.
  • Does a value occurring once in the whole array necessarily qualify? No; that position may belong to several length- k subarrays.
  • What happens when k equals the array length? There is only one subarray, so every value appearing in the array qualifies.

What a Strong Answer Covers Guidance

  • Counts distinct subarrays containing each value, without counting repeated copies within a subarray more than once.
  • Applies the exactly-one condition and returns the largest eligible value.
  • Handles overlapping subarrays, k = 1 , k = nums.length , and the absence of an eligible value.
  • Gives bounds appropriate for the stated array length and value range.

Follow-up Questions Guidance

  • For k = 1 , how does this definition relate to the frequency of a value in the array?
  • Why does counting array occurrences give a different result from counting containing subarrays?