Run-Length Compression of an Integer
Updated
Problem statement
Given a nonnegative integer number, compress the digits of its ordinary decimal representation from left to right.
Split the decimal digits into groups of consecutive equal digits. Start a new group only when the digit changes. For each group:
- Write the digit once.
- If the group contains more than one digit, write its full count after the digit. If it contains just one digit, do not write a count.
Join the encoded groups in order and return the result as an integer. The input and return value are integers, not strings or arrays. The integer 0 has the one-digit representation 0 and compresses to 0.
Encode each original group separately, even if this places matching digits next to each other in the result. Compress exactly once; do not compress the result again. You do not need to recover the original number from the result, and the result may be larger than the input.
Function
compressInteger(number: long) → longExamples
Example 1
number = 11122return = 1322The runs are 111 and 22. They encode as 13 and 22, so the returned integer is 1322.
Example 2
number = 1000return = 103The singleton 1 contributes 1, and the three zeros contribute 03. Concatenating them gives 103; the zero within that encoding must be preserved.
Example 3
number = 111111111111return = 112Twelve consecutive copies of the digit 1 contribute the digit 1 followed by the two-digit count 12.
Example 4
number = 0return = 0Zero has one decimal digit. Its run has length one, so it remains 0.
Constraints
0 <= number <= 10^18 - 1.- Use the ordinary decimal representation, without a sign or leading zeros except for
0itself. - The input and compressed result fit in a signed 64-bit integer.
- You may use strings internally. As a follow-up, solve the problem using arithmetic and
O(1)auxiliary space.
More Bloomberg LP Phone Screen questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE 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
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Subtract Two Large Number StringsStackAdapt · PHONE SCREENStackAdapt interview questions
- Transaction Fee Calculator — Parts 1–2: Basic Fee Calculation and Conditional Fee RulesStripe · PHONE SCREENStripe interview questions
- Fair Coin from a Biased CoinAnthropic · PHONE SCREENAnthropic interview questions
- Stack Formula InterpreterTypeSafe AI · PHONE SCREEN
- Roman to IntegerKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Minimum Difference Between Two Clock TimesMicron Technology · PHONE SCREEN
- Decimal Digits in OrderZoomInfo · PHONE SCREEN
- Add Two Integers Given as StringsJane Street · PHONE SCREENJane Street interview questions
- Fizz and Buzz Words From 1 to nDocuSign · PHONE SCREENDocuSign interview questions
- Integer to String Without Built-insHudson River Trading · PHONE SCREENHudson River Trading 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)