Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)
Updated
Problem statement
Given an integer array fruits and an integer k, return the number of contiguous sections of fruits in which you can form at least k pairs of identical fruits.
The fruits lie on a conveyor belt, and fruits[i] is the type of the fruit at position i. A pair is two fruits of the same type at two different positions of the section. Each fruit can be part of at most one pair when you count toward the k pairs.
Note: a section that holds c fruits of one type can form c / 2 pairs of that type, rounded down. For example, three fruits of type 0 form only one pair.
Function
solution(fruits: int[], k: int) → longExamples
Example 1
fruits = [0,1,0,1,0]k = 2return = 3There are 3 contiguous sections with at least k = 2 pairs of identical fruits:
fruits[0..3] = [0, 1, 0, 1]: one pair of type0(positions0and2) and one pair of type1(positions1and3).fruits[1..4] = [1, 0, 1, 0]: one pair of type1(positions0and2inside the section) and one pair of type0(positions1and3inside the section).fruits[0..4] = [0, 1, 0, 1, 0]: one pair of type0(for example indices0and2) and one pair of type1(indices1and3). The0s at indices0and2and the0s at indices2and4cannot both be used as pairs, because they share the fruit at index2. So the three0s give only one pair; two pairs would need four fruits.
Example 2
fruits = [2,2,2,2,2,2]k = 3return = 1Only the whole belt works: its six fruits of type 2 form 3 pairs. Every shorter section has at most five of them, which is at most 2 pairs.
Example 3
fruits = [4,4,4,9,9]k = 2return = 2The sections [4, 4, 4, 9, 9] and [4, 4, 9, 9] each have one pair of 4s and one pair of 9s. The section [4, 4, 4, 9] has three 4s, but they form only one pair, so it is not counted.
Constraints
1 <= fruits.length <= 10^50 <= fruits[i] <= 10^91 <= k <= fruits.length
More Boston Consulting Group interview questions
- Build Taxi Driver Performance FeaturesOA · Seen Nov 2026
- Summarize Taxi Drivers and RidesOA · Seen Nov 2026
- Competition Winner and Runner-Up (Forward Deployed Engineer)OA · Seen Nov 2026
- Robot Stops Left or Right (Forward Deployed Engineer)OA · Seen Nov 2026
- Split a Matrix Into Four Partitions by Average (Forward Deployed Engineer)OA · Seen Nov 2026
- Predict Taxi Driver ClassesOA · Seen Oct 2026
- Prepare Taxi Driver Classification DataOA · Seen Oct 2026
- Even-Position MonotonicityOA · Seen Jul 2026
- Shortest Distances From an Adjacency MatrixPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Shortest Window with Required CountsOracle · ONSITE INTERVIEWOracle interview questions
- Count Subarrays With Product Equal to KSalesforce · OASalesforce interview questions
- Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
- Count Subarrays with at Least K Equal-Fruit PairsTikTok · OATikTok interview questions
- Consistent LogsRippling · OARippling interview questions
- Minimum Length Good SubarrayMicrosoft · OAMicrosoft interview questions
- Auction Sniping Window MinimumCisco · OACisco interview questions
- Balanced Prefix Sets in a PermutationUber · OAUber interview questions
- Shortest Subarray Covering an Integer RangeMarshall Wace · OAMarshall Wace interview questions
- Count Subarrays with at Least K Distinct ValuesVisa · OAVisa interview questions