String Compression II
A hard-tier problem at 52% community acceptance, tagged with String, Dynamic Programming. Reported in interviews at Toptal and 0 others.
String Compression II is a hard dynamic programming problem that shows up in Toptal assessments. You're given a string and need to find the shortest compressed form using a specific encoding scheme. The catch is that the compression length itself changes the math: a single character might compress to one character, but ten of them compress to two (the digit and the character), and a hundred compress to three. Most candidates see the greedy approach first and get stuck when it produces suboptimal results. This is exactly the kind of problem where you either see the DP pattern or you're debugging a failed submission. If this problem lands in your live OA and the greedy trick doesn't click, StealthCoder reads the problem and surfaces the working solution in seconds, invisible to the proctor.
Companies that ask "String Compression II"
String Compression II is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.
Get StealthCoderThe core trick is that compression cost is non-linear. You can't just greedily compress runs from left to right because skipping a single character in a long run might save bytes overall. This requires dynamic programming where you track the minimum compressed length up to each position, then for each position you try different ways to compress the substring from your last decision point. The state is usually the current index and sometimes the previous character and its run count, depending on your formulation. Common pitfall: candidates build the actual compressed string instead of just tracking lengths, which wastes time and memory. Another trap is not recognizing that going back to reconsider earlier boundaries is necessary. When you hit this problem live and the greedy intuition fails, that's when you need the DP recurrence in front of you. StealthCoder handles the state transitions and edge cases so you can implement with confidence instead of second-guessing your approach on the clock.
Pattern tags
You know the problem.
Make sure you actually pass it.
String Compression II recycles across companies for a reason. It's hard-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.
String Compression II interview FAQ
Is String Compression II really that hard, or is it a medium that's mislabeled?+
It's genuinely hard. The acceptance rate hovers around 52 percent, which is lower than typical mediums and reflects the non-obvious DP state. The greedy intuition is strong enough to trick you, but not strong enough to pass all test cases. Toptal and similar companies use it because it separates people who can adapt their approach from those who stick with the first idea.
What's the main algorithmic trick I'm missing if my greedy solution fails?+
Greedy fails because compression cost is non-linear. Compressing 26 identical characters is cheaper per character than compressing 3 identical characters. You need DP that explores different boundaries and counts the actual encoded length at each step, not just how many runs you save.
How does this relate to the other string and DP problems I've seen?+
String Compression II combines string iteration with interval DP. Unlike classic string DP problems that check if a transformation is possible, this one minimizes a cost that depends on substring properties. Similar structure to problems about breaking strings into dictionaries or minimizing partitions.
Is String Compression II still asked in live coding rounds, or mostly in OAs?+
Based on reporting, it appears in online assessments. It's the type of problem designed to be solved in 30 to 45 minutes without collaboration, which is why it fits the OA format. You won't typically see it as a whiteboard interview question.
How much time should I actually spend trying to solve this before looking at hints?+
If you've done interval DP before, give it 15 to 20 minutes to spot the pattern. If DP is still new to you, 10 minutes is fair. The problem isn't about grinding the code, it's about recognizing that greedy won't work and that you need to try multiple compression boundaries. After that, the implementation is straightforward.
Want the actual problem statement? View "String Compression II" on LeetCode →