FastPrepCount Subarrays with at Least K Distinct Values
Problems/Count Subarrays with at Least K Distinct Values

Count Subarrays with at Least K Distinct Values

Visa logoVisa● MediumINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: An August 28, 2025 Visa SDE Intern CodeSignal OA report gives the goal on contiguous subarrays, the at-least-k threshold, and the rule that repeats do not raise the distinct count. It leaves out limits, return width, and a function signature. Our version adds rules for 64-bit numbers.

You are given an integer array nums and an integer k.

Return the number of contiguous subarrays that contain at least k distinct values. Repeated occurrences of the same value count only once toward a subarray's distinct-value total.

Function

countSubarraysWithAtLeastKDistinct(nums: int[], k: int) → long

Examples

Example 1

nums = [1,2,1]k = 2return = 3

The valid subarrays are [1,2], [2,1], and [1,2,1].

Example 2

nums = [1,1,1]k = 2return = 0

Every subarray contains only the value 1, so none has two distinct values.

Constraints

  • 1 <= nums.length <= 2 * 105
  • -109 <= nums[i] <= 109
  • 1 <= k <= nums.length
  • The answer fits in a signed 64-bit integer.

More Visa Online Assessment questions

  • Count Prime Values in SubtreesOA · Seen Nov 2026
  • Lock CodeOA · Seen Nov 2026
  • Minimum-Sum Tree Fill with Unit StepsOA · Seen Nov 2026
  • Longest Non-Decreasing Substring After Pair SwapsOA · Seen Oct 2026
  • Count Attack Vectors by Divisor RangeOA · Seen Oct 2026
  • Minimum Operations to Reduce a Transaction LedgerOA · Seen Oct 2026
  • Count Valid Space-Delimited WordsOA · Seen Oct 2026
  • Earliest Common Meeting StartOA · Seen Oct 2026
  • Maximum Index-Weighted Sum with Disjoint SwapsOA · Seen Oct 2026
  • Maximum Equalized Substrings After One Adjacent SwapOA · Seen Sep 2026

Similar questions at other companies

  • Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)Boston Consulting Group · OA
  • Count Servers With No Requests in a Time WindowIBM · OA
  • Count Subarrays with at Least K Equal-Fruit PairsTikTok · OA
  • Consistent LogsRippling · OA
  • Minimum Length Good SubarrayMicrosoft · OA
  • Auction Sniping Window MinimumCisco · OA
  • Balanced Prefix Sets in a PermutationUber · OA
  • Shortest Subarray Covering an Integer RangeMarshall Wace · OA
  • Longest Frequency-Bounded SubarrayZipRecruiter · OA
  • Find Subarray With Minimum Distinct IntegersAmazon · OA
See Visa hiring insights
Saved
class Solution {
    public long countSubarraysWithAtLeastKDistinct(int[] nums, int k) {
        // Write your code here.
    }

}
Checking account…