Minimum Distinct Prefix Cost
Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Goldman Sachs reported this one in September 2026, and the whole thing hinges on a frequency map. You get an array, you can shuffle it any way you like, and you minimize the sum of distinct counts across every prefix. It looks like a permutation search, and that's the trap. It's really counting plus a sort. Once you see where the cost actually comes from, the code is about ten lines. If you've got the OA in a day or two, learn the one insight and you're fine. If your brain locks up mid-assessment, StealthCoder sits invisibly on your screen as a safety net and hands you the approach.
The problem
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
Reported by candidates. Source: FastPrep
Pattern and pitfall
Here's the trick. Keep equal values together in one block. The distinct count only goes up at the first occurrence of each value. A value whose first occurrence sits at 0-indexed position p adds (n - p) to the total. So you want first occurrences as late as possible. Each block's start is the sum of the sizes of the blocks before it. Big blocks placed early push every later start further right. So count frequencies with a hash map, sort them descending, and walk through them. Cost is n times the number of distinct values, minus the sum of the block starts. Check with [2,2,3,1,1]: frequencies 2,2,1, starts 0,2,4, so 15 - 6 = 9. Pitfall: use a 64-bit sum, since n reaches 10^5. Another pitfall is sorting ascending by instinct. If you freeze during the live OA, StealthCoder is the hedge that surfaces this reasoning.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Minimum Distinct Prefix Cost cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Goldman Sachs's OA.
Goldman Sachs reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Minimum Distinct Prefix Cost FAQ
How hard is Minimum Distinct Prefix Cost really?+
Medium on paper, easy once you spot the greedy. The code is a frequency count, a sort and a loop. The difficulty is realizing you never need to try permutations. If you see that cost depends only on where each value first appears, it takes about ten minutes.
What's the trick to solve it?+
Group identical values into contiguous blocks, then order the blocks by frequency from largest to smallest. Each distinct value costs (n - start position of its block). Larger blocks first push later blocks' starts right, which shrinks the total. Sorting frequencies descending is the entire algorithm.
Why do equal values have to stay together?+
Splitting a value's occurrences never helps. The extra copies don't add to the distinct count, but they take up space that could delay a different value's first appearance. Pushing duplicates forward, right behind their first occurrence, delays every later new value. So contiguous blocks are never worse.
What complexity does the Goldman Sachs OA expect here?+
The constraints go to 10^5, so O(n log n) is comfortable. Counting is O(n), and sorting the distinct frequencies is at most O(n log n). Use a long for the result, because the sum can reach roughly n squared over two, which overflows a 32-bit int.
How do I prepare for this in 48 hours?+
Practice the frequency-map-then-sort pattern on a few problems, and hand-trace the example [2,2,3,1,1] until you get 9. Then write the formula: n times distinct count, minus the sum of block starts. Test an all-equal array and an all-distinct array as edge cases before you submit.