FastPrepCompressed Representation Size
Problems/Compressed Representation Size

Compressed Representation Size

Microsoft logoMicrosoft● HardOA

Updated Sep 2026

QuestionFunctionExamples

Problem statement

Source note: 2026-07-17 — Example 2 had an extra C in the input, so it did not match the shown removal or answer. The input was corrected. The judged core task matches the source intent at about 99%.

Strings with long blocks of repeating characters take much less space if kept in a compressed representation. To obtain the compressed representation, we replace each segment of equal characters in the string with the number of characters in the segment followed by the character (for example, we replace segment "CCCC" with "4C"). To avoid increasing the size, we leave the one-letter segments unchanged (the compressed representation of "BC" is the same string - "BC".

For example, the compressed representation of the string "ABBBCDDCCC" is "A3B2C2D3C", and the compressed representation of the string "AAAAAAAAAAABXXAAAAAAAAAA" is "11AB2X10A".

Observe that, in the second example, if we removed the "BXX" segment from the middle of the word, we would obtain a much shorter compressed representation - "21A". In order to take advantage of this observation, we decided to modify our compression algorithm. Now, before compression, we remove exactly K consecutive letters from the input string. We would like to know the shortest compressed form that we can generate this way.

Given a string S of length N and an integer K, returns the shortest possible length of the compressed representation of S after removing exactly K consecutive characters from S.

Function

compressedRepresentationSize(s: String, k: int) → int

Examples

Example 1

s = "ABBBCCDDCCC"k = 3return = 5

(Remove DDC -> A3B4C)

Example 2

s = "ABCDDDEFG"k = 2return = 6

Remove EF. The remaining string is ABCDDDG, whose compressed representation is ABC3DG and has length 6.

More Microsoft interview questions

  • Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
  • Authentication SystemOA · Seen Nov 2026
  • Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
  • Maximum String OperationsOA · Seen Nov 2026
  • Minimum Cost to Process Priority TasksOA · Seen Nov 2026
  • Most Visited Sprint MarkerOA · Seen Nov 2026
  • Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
  • Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
  • Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
  • Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Lexicographically Smallest Grid Path with One Letter ChangeInfosys · OAInfosys interview questions
  • Minimum Merge ConflictsAmazon · OA
  • String Formation (Also for AI/ML Software Engineer Intern :)Snowflake · OASnowflake interview questions
  • Longest Subsequence which is a SubstringSalesforce · OASalesforce interview questions
  • Distinct MovesMeesho · OA
  • Find Minimum OperationsIBM · OAIBM interview questions
  • String Challenge (Generic)TikTok · OATikTok interview questions
  • Maximize the Lottery IDCitadel · OACitadel interview questions
  • Sum of All ValuesFlexport · OAFlexport interview questions
  • Maximum Crayon Removal ScoreWells Fargo · OAWells Fargo interview questions

Microsoft interview experiences

  • Microsoft OA Questions, October 2026: 4 Experiences
  • Microsoft Experienced SWE Phone Screen: 1 Experience
  • Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
  • Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
  • Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
  • Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int compressedRepresentationSize(String s, int k) {
      // write your code here
    }

}
Checking account…