String Compression
Updated
Problem statement
Given a string chars, compress each maximal run of equal consecutive characters.
- Write the character once.
- If the run length is greater than one, immediately follow it with the decimal digits of that length.
Return the complete compressed string. Run counts may contain multiple digits.
Function
compress(chars: String) → StringExamples
Example 1
chars = "aabbccc"return = "a2b2c3"The consecutive runs have lengths 2, 2, and 3.
Example 2
chars = "a"return = "a"A run of length one has no numeric suffix.
Example 3
chars = "abbbbbbbbbbbb"return = "ab12"The first run has length one, and the twelve consecutive b characters use the two digits 12.
Constraints
1 <= chars.length <= 100000.charscontains printable ASCII characters other than line breaks.
More Bloomberg LP Phone Screen questions
- Collatz Conjecture with Shared MemoizationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Longest Palindromic SubstringSiemens · PHONE SCREEN
- Shortest Deletion for Distinct CharactersOracle · ONSITE INTERVIEWOracle interview questions
- Reverse Words in a StringAgoda · PHONE SCREENAgoda interview questions
- Longest Palindromic SubstringIBM · PHONE SCREENIBM interview questions
- Word Collection: Distance and AnagramsGoogle · ONSITE INTERVIEWGoogle interview questions
- Shortest Way to Form a Target StringPinterest · PHONE SCREENPinterest interview questions
- Validate a Palindrome After Limited DeletionsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Reverse File LinesCitadel · ONSITE INTERVIEWCitadel interview questions
- Longest Palindromic Substring LengthMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
Interview reports that mention this question
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced Design & More: 126 Questions (Part 2 of 2)
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)