FastPrepShortest Compressed Length After Removal
Problems/Shortest Compressed Length After Removal

Shortest Compressed Length After Removal

DRW logoDRW● MediumINTERNOA

Updated Nov 2026

QuestionFunctionExamples

Problem statement

Source note: The report starts after the formal definition of compression and ends before the number limits. Its three worked examples show the compression format. Our version is about 97% the same as the reported question.

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 = 3return = 5

Remove DDC to obtain ABBBCCCC. It compresses to A3B4C, whose length is 5.

Example 2

S = "AAAAAAAAAAABXXAAAAAAAAAA"K = 3return = 3

Remove BXX to leave twenty-one consecutive A characters. They compress to 21A, whose length is 3.

Example 3

S = "ABCDDDEFG"K = 2return = 6

Remove EF to obtain ABCDDDG. It compresses to ABC3DG, whose length is 6.

More DRW Online Assessment questions

  • Count Balanced Nodes in a Rooted TreeOA · Seen Sep 2026
  • Largest Number With Even Digit FrequenciesOA · Seen Sep 2026
  • Shortest Path Around Rectangular ObstaclesOA · Seen Sep 2026
  • 2020 Account Balance with Monthly Card FeesOA · Seen Aug 2026
  • Nested In-Memory Database TransactionsOA · Seen Aug 2026
  • Password Requirement CheckOA · Seen Aug 2026
  • Remove Excess Consecutive CharactersOA · Seen Aug 2026
  • Last Round for Each PlayerOA · Seen Jul 2026
  • Doctor Appointment Slot AssignmentOA · Seen Jul 2026
  • Maximum Even-Sum Neighboring PairsOA · Seen Jul 2026

Similar questions at other companies

  • Earliest Time to Schedule a MeetingMathWorks · OAMathWorks interview questions
  • Earliest Meeting Start on a 24-Hour ClockIMC Trading · OAIMC Trading interview questions
  • Earliest Time to Schedule a MeetingMicrosoft · OAMicrosoft interview questions
  • Maximum Equalized Substrings After One Adjacent SwapVisa · OAVisa interview questions
  • Maximum Redundancy After One Segment FlipBloomberg LP · OABloomberg LP interview questions
  • Balanced Split String with WildcardsTwo Sigma · OATwo Sigma interview questions
  • Longest Balanced Substring After One SwapTekion · OATekion interview questions
  • Count Special StringsAmazon · OA
  • Binary Palindromic Subsequences of Length 5Citadel · OACitadel interview questions
  • Maximum Positive FeedbackTikTok · OATikTok interview questions
All DRW interview questionsSee DRW hiring insights
Saved
class Solution {
    public int solution(String S, int K) {
        // write your code here
    }

}
Checking account…