Reported July 2026
DRWsliding window

Shortest Compressed Length After Removal

Reported by candidates from DRW's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live DRW OA. Under 2s to a working solution.
Founder's read

The DRW OA reported in July 2026 looks like a string compression warm-up, then bites you on the edge case. You remove exactly K consecutive characters, and the leftover pieces on each side can merge into one bigger run. That merge is what trips up a naive solution. Try every window start, and you're looking at a sliding window over the string with run-length bookkeeping. If you blank on the merge logic mid-assessment, StealthCoder runs invisibly on your desktop and gives you a working solution in real time. Better to understand the trick first, though.

The problem

A compressed representation replaces each maximal run of equal consecutive characters as follows:
A run of length 1 is represented by the character alone.
A run longer than 1 is represented by its decimal length followed by the character.
For example, ABBBCCCC compresses to A3B4C.
Given a string S of length N and an integer K, remove exactly K consecutive characters from S. Return the shortest possible length of the compressed representation of the remaining string.
The brackets in each HTML diagram mark the consecutive characters removed in that example.
┌───────────────┐ ┌───────┐
│ ABBBCC[DDC]CC │ ──▶ │ A3B4C │
└───────────────┘ └───────┘
┌──────────────────────────┐ ┌─────┐
│ AAAAAAAAAAA[BXX]AAAAAAAAAA │ ──▶ │ 21A │
└──────────────────────────┘ └─────┘
┌────────────┐ ┌───────┐
│ ABCDDD[EF]G │ ──▶ │ ABC3DG │
└────────────┘ └───────┘

Function
solution(S: String, K: int) → int

Examples
Example 1
S = "ABBBCCDDCCC"
K = 3
return = 5
Remove DDC to obtain ABBBCCCC. It compresses to A3B4C, whose length is 5.
Example 2
S = "AAAAAAAAAAABXXAAAAAAAAAA"
K = 3
return = 3
Remove BXX to leave twenty-one consecutive A characters. They compress to 21A, whose length is 3.
Example 3
S = "ABCDDDEFG"
K = 2
return = 6
Remove EF to obtain ABCDDDG. It compresses to ABC3DG, whose length is 6.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that removing a window of K characters can join the character just before it with the character just after it. If they match, their runs fuse, and the compressed length changes in a way a per-window recount misses. Example 2 shows it: removing BXX leaves 11 A's and 10 A's merging into 21A. The brute force is O(N*K) or O(N^2): for each of N-K+1 start positions, build the remaining string and compress it. That's fine for small N. The faster route precomputes run-length encodings for the prefix and suffix, then combines them at the boundary, adding the merged run's digit count once. Pitfall: the digit count. A run of 9 costs 2 characters, a run of 10 costs 3, and a run of 1 costs just 1. Merging can push a run across one of those thresholds. If the live OA has you freezing on the boundary math, StealthCoder is the hedge that keeps you moving.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Shortest Compressed Length After Removal 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass DRW's OA.

DRW reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Shortest Compressed Length After Removal FAQ

What's the trick in the DRW shortest compressed length problem?+

Removing a window can make the characters on either side touch. If they're equal, two runs fuse into one, and the length cost changes. Handle that boundary explicitly. Everything else is just standard run-length encoding for the prefix and suffix.

How hard is this DRW OA question really?+

Medium. The idea is simple, but the details bite. Run length costs aren't linear: 1 costs 1, 2 through 9 cost 2, 10 through 99 cost 3. Get the digit counting wrong and the examples will still pass until a hidden case fails.

Can I just brute force it?+

Yes, if N is small. Try each of the N-K+1 windows, build the leftover string, compress it, and take the minimum. That's O(N^2) overall. You don't know the hidden constraints, so know the prefix and suffix approach in case brute force times out.

What edge cases should I test before submitting?+

Test K equal to N, which leaves an empty string and returns 0. Test removals that merge two runs, like example 2. Test runs that cross a digit threshold, such as 9 to 10. Also test a string of one repeated character.

How do I prepare for this in 48 hours?+

Write run-length encoding from scratch until it's automatic, including the length-1 rule. Then code the brute force window loop and verify against the three examples. Only after that, optimize with prefix and suffix runs. Don't start with the optimized version.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with DRW.

OA at DRW?
Invisible during screen share
Get it