All questions
Intermediate · Java · Strings
Java: run-length encode a string
Java coding interview preparation for QA and SDET roles · ~12 min
This is a Pro program. Practice and solutions require ₹99/month. New coding programs are added every month. Basic programs remain free.
Problem statement
Implement static String compress(String s).
Encode each consecutive character run as character followed by count, including runs of length one.
Input & output
compress("aaabbc") → a3b2c1Interview discussion
- Explain the time and auxiliary-space complexity of your approach.
- Which boundary cases matter when testing run-length encode a string?
Java 8 · Strings. Implement the required static method in class Solution. The test runner checks returned values, including boundary cases; printing a sample alone does not solve the exercise.
More Java coding questions
- Java: longest substring without repeating charactersAdvanced · Sliding window
- Java: find the longest palindromic substringAdvanced · Strings
- Java: count overlapping substring occurrencesIntermediate · Strings
- Java: validate signed integer textIntermediate · Strings
