Run-Length String Compression
Updated
Problem statement
Given a string text, encode each maximal run of equal consecutive characters from left to right.
- Append the run's character once.
- If the run length is greater than
1, append the full length in base 10. - If the run length is exactly
1, append no count.
Return the concatenation of all encoded runs. Return the empty string when text is empty.
Function
compressRuns(text: String) → StringExamples
Example 1
text = "aaabbccccd"return = "a3b2c4d"The maximal runs are aaa, bb, cccc, and d. The singleton contributes only its character.
Example 2
text = ""return = ""An empty input has no runs and produces an empty encoding.
Example 3
text = "zzzzzzzzzzzzx"return = "z12x"The first run demonstrates a multi-digit count. The final singleton x has no count.
Constraints
0 <= text.length <= 200000textcontains printable ASCII characters.- The returned encoding is formed exactly as specified; it does not need to be shorter than the input.
More Oracle Onsite Interview questions
- Course ScheduleONSITE INTERVIEW · Seen Nov 2026
- Shortest Subarray with Sum at Least KONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Minimum Meeting RoomsONSITE INTERVIEW · Seen Nov 2026
- Shortest Window with Required CountsONSITE INTERVIEW · Seen Nov 2026
- Maximum Subarray Sum with One DeletionONSITE INTERVIEW · Seen Nov 2026
- Shortest Deletion for Distinct CharactersONSITE INTERVIEW · Seen Nov 2026
- Count Dropped Rate-Limited RequestsONSITE INTERVIEW · Seen Nov 2026
- Design Authentication ManagerONSITE INTERVIEW · Seen Oct 2026
- Maximum Bouquet ValueONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- String CompressionShnoor International · ONSITE INTERVIEW
- Longest Spoken PrefixElevenLabs · ONSITE INTERVIEWElevenLabs interview questions
- Palindrome Check Ignoring SymbolsWalmart · ONSITE INTERVIEWWalmart interview questions
- Reverse a String Keeping Dashes in PlaceNykaa · ONSITE INTERVIEWNykaa interview questions
- Reverse Vowels of a StringGoogle · ONSITE INTERVIEWGoogle interview questions
- Valid Palindrome After NormalizationKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Count Binary SubstringsDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Minimum Changes for Palindromic BlocksCitadel · OA · PHONE SCREEN · ONSITE INTERVIEWCitadel interview questions
- Palindrome Match PercentageApple · ONSITE INTERVIEWApple interview questions
- Reverse A StringNVIDIA · ONSITE INTERVIEWNVIDIA interview questions