FastPrepCount Belt Sections With K Fruit Pairs (Forward Deployed Engineer)
Problems/Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)

Count Belt Sections With K Fruit Pairs (Forward Deployed Engineer)

Boston Consulting Group logoBoston Consulting Group● MediumFULLTIMEOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Boston Consulting Group online assessment for a Forward Deployed Engineer role, as one of four coding tasks. The task was to count the stretches of a fruit conveyor belt where at least k pairs of matching fruits can be made, with each fruit used in at most one pair.

The report gives the full rule, the first example, and the input of the second example. The second example's answer was cut off, and no limits were shown. We worked out that answer as 1 and chose up to 10^5 fruits, fruit types from 0 to 10^9, and k from 1 to the belt length. The answer is returned as a 64-bit number. The third example is our own.

Our version is about 90% the same as the reported question.

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) → long

Examples

Example 1

fruits = [0,1,0,1,0]k = 2return = 3

There are 3 contiguous sections with at least k = 2 pairs of identical fruits:

  • fruits[0..3] = [0, 1, 0, 1]: one pair of type 0 (positions 0 and 2) and one pair of type 1 (positions 1 and 3).
  • fruits[1..4] = [1, 0, 1, 0]: one pair of type 1 (positions 0 and 2 inside the section) and one pair of type 0 (positions 1 and 3 inside the section).
  • fruits[0..4] = [0, 1, 0, 1, 0]: one pair of type 0 (for example indices 0 and 2) and one pair of type 1 (indices 1 and 3). The 0s at indices 0 and 2 and the 0s at indices 2 and 4 cannot both be used as pairs, because they share the fruit at index 2. So the three 0s give only one pair; two pairs would need four fruits.

Example 2

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

Only 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 = 2

The 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^5
  • 0 <= fruits[i] <= 10^9
  • 1 <= 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
All Boston Consulting Group interview questionsSee Boston Consulting Group hiring insights
Saved
class Solution {
    public long solution(int[] fruits, int k) {
        // Write your code here.
    }
}
Checking account…