Better Compression
Reported by candidates from Goldman Sachs's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Goldman Sachs hit candidates with this string compression problem in January 2024, and the title alone is a trap. You're not building a new compression algorithm. The OA is asking you to parse an already-compressed string (format: character counts like 'a3b2'), then reorder and output it in a specific way. It's deceptively simple until you realize the reordering rule. StealthCoder can spot the pattern in seconds if you freeze mid-implementation.
The problem
\ Function Description\ Complete the function betterCompression in the editor. betterCompression has the following parameter: \ Return\ string: the properly compressed string \
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick here is that 'better compression' means taking a mixed-order compressed string and outputting it sorted by character, with counts aggregated if the same character appears multiple times in the input. Parse the input by pulling out characters and their counts, merge duplicates into a hash table, sort by character lexicographically, then reconstruct. The gotcha is handling edge cases like missing counts or malformed input. This is a parsing and sorting problem wrapped in a string. StealthCoder will validate your sort order and catch off-by-one bugs in reconstruction when you're live.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Better Compression 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as group anagrams. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Goldman Sachs's OA.
Goldman Sachs reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Better Compression FAQ
What does 'better' mean in the problem title?+
It means the output is sorted alphabetically by character and deduplicated. If input is 'b2a3b1', output is 'a3b3'. The 'better' compression is the cleaned, sorted version. Don't overthink the name.
Do I need to handle uppercase vs lowercase separately?+
The problem statement doesn't specify. Assume they're different characters unless the examples show otherwise. Sort them ASCII order (uppercase before lowercase) unless told to case-fold.
What if the input has no counts or malformed chunks?+
Treat it as count 1 if missing. If a character has no number after it, assume count is 1. The problem doesn't mention invalid input, so assume input is well-formed or forgiving.
Is this a dynamic programming problem?+
No. It's a hash table and sorting problem. Parse, aggregate counts, sort, reconstruct. One pass through input, O(n log n) for sort. Don't overthink it.
How do I prepare in 24 hours?+
Understand string parsing in your language. Write a quick hash table to count, then sort by key. Test on a simple example like 'b2a3' to output 'a3b2'. The implementation is 15-20 lines if you know the pattern.