FastPrepMinimum Distinct Prefix Cost
Problems/Minimum Distinct Prefix Cost

Minimum Distinct Prefix Cost

Chubb logoChubb● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A report says the cost of each position is the number of distinct values in its prefix, and allows any reordering. We chose the function signature, the examples, the limits, and a 64-bit result.

Special thanks: 🌷 A special thank you to a lovely, amazing fren who shared that this question appeared again on October 10, 2026! ( ᴗ͈ˬᴗ͈)ഒ 💜

You are given an integer array arr of length n.

The cost of an array is the sum, over all of its prefixes, of the number of distinct values in each prefix. In other words, for every index i, count the distinct values in arr[0..i] and add all of those counts.

You may rearrange arr in any order. Return the minimum possible cost among all permutations of arr.

Function

minimumDistinctPrefixCost(arr: int[]) → long

Examples

Example 1

arr = [2,2,3,1,1]return = 9

One optimal permutation is [2,2,1,1,3]. Its prefix distinct counts are 1, 1, 2, 2, 3, so the total cost is 1 + 1 + 2 + 2 + 3 = 9.

Constraints

  • 1 <= n <= 10^5
  • 1 <= arr[i] <= 10^5

More Chubb Online Assessment questions

  • Count Pairs with K Divisible Values in the IntervalOA · Seen Oct 2026
  • Index of the Maximum After Left RotationsOA · Seen Oct 2026
  • Maximize Revenue from Decreasing InventoryOA · Seen Oct 2026
  • Minimize the Maximum Array ElementOA · Seen Oct 2026
  • Flat JSON Value DifferencesOA · Seen Sep 2026
  • Maximum Student-Teacher DistanceOA · Seen Sep 2026
  • Minimum Cost to Reach a Target WeightOA · Seen Sep 2026
  • Minimum Last-Element RelocationsOA · Seen Sep 2026
  • Most Visited Sprint MarkerOA · Seen Sep 2026
  • How Many Sentences?OA · Seen Sep 2026

Similar questions at other companies

  • Minimum Distinct Prefix CostGoldman Sachs · OAGoldman Sachs interview questions
  • Rearrange Songs by AuthorZomato / Eternal · OAZomato / Eternal interview questions
  • Find Max Health SumAmazon · OA
  • Nickname-Constrained GroupingZoox · ONSITE INTERVIEW
  • Partition Into K-Size Consecutive GroupsDream11 · ONSITE INTERVIEWDream11 interview questions
  • Find the Original Array From a Doubled ArrayGoogle · PHONE SCREENGoogle interview questions
  • API Rate Limit Violation DetectionTekion · OATekion interview questions
  • Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
  • Count Servers With No Requests in a Time WindowIBM · OAIBM interview questions
  • Maximum Escape Game ScoreMicrosoft · OAMicrosoft interview questions
All Chubb interview questionsSee Chubb hiring insights
Saved
class Solution {
    public long minimumDistinctPrefixCost(int[] arr) {
        // Write your code here.
    }

}
Checking account…