Essential cookies keep authentication working. With your permission, we also use analytics cookies to understand and improve the product. Read our Privacy Policy

DataEngPrep.tech
QuestionsPracticeAI CoachDashboardPricingBlog
ProLogin
Home/Questions/Python/Coding/Given the input string "AAABBBCCCDDDAAA," compress it to output "A3B3C3D3A3."

Given the input string "AAABBBCCCDDDAAA," compress it to output "A3B3C3D3A3."

Python/Codingeasy2 min read

Reviewed by Aditya Kumar · Last reviewed 2026-03-24

This problem describes Run Length Encoding (RLE) , a simple, lossless data compression technique. It works by replacing sequences of identical data values with a single data value and its count.…

🤖 Analyze Your Answer
Frequency
Low
Asked at 1 company
Category
179
questions in Python/Coding
Difficulty Split
127E|24M|28H
in this category
Total Bank
1,863
across 7 categories
Asked at these companies
S&P Global

Why This Question Matters

This easy-level Python/Coding question appears frequently in data engineering interviews at companies like S&P Global. While less common, it tests deeper understanding that distinguishes strong candidates.

How to Approach This

Start by clearly defining the core concept being asked about. Interviewers want to see that you understand the fundamentals before diving into implementation details. Structure your answer with a definition, then explain the practical application with a concise example. The expert answer includes a code example that demonstrates the implementation pattern.

Expert Answer
345 wordsIncludes code

This problem describes Run-Length Encoding (RLE), a simple, lossless data compression technique. It works by replacing sequences of identical data values with a single data value and its count.

Mechanics and Why it's Used

RLE encodes data by iterating through a sequence, identifying consecutive runs of the same character, and then storing the character and the length of its run. For the input "AAABBBCCCDDDAAA," the process is:

  • 'A' appears 3 times: "A3"

  • 'B' appears 3 times: "B3"

  • 'C' appears 3 times: "C3"

  • 'D' appears 3 times: "D3"

  • 'A' appears 3 times: "A3"

  • Concatenating these yields "A3B3C3D3A3." Decoding is the reverse: read a character, then its count, and repeat the character that many times.

    RLE is valuable for its simplicity and effectiveness on data with long runs of identical values. In data engineering, it's frequently used internally within columnar storage formats like Parquet and ORC, especially for columns with low cardinality or those that are sorted, where it significantly reduces storage footprint and improves query performance by minimizing I/O. It can also be implicitly leveraged in systems like Snowflake (within micro-partitions) or Spark (during shuffle operations where data might be grouped or sorted), as it optimizes the underlying storage of repetitive data.

    def run_length_encode(s: str) -> str:
        if not s: return ""
        encoded_string = []
        i = 0
        while i < len(s):
            j = i
            while j < len(s) and s[j] == s[i]:
                j += 1
            encoded_string.append(f'{s[i]}{j - i}')
            i = j
        return "".join(encoded_string)
    

    Trade-offs and Interview Considerations

    While effective for repetitive data, RLE can actually increase data size if the input has very few or no consecutive repetitions (e.g., "ABC" becomes "A1B1C1"). This highlights its specific use case.

    In the interview, also mention handling single characters. Some implementations might output "A1" for a single 'A', while others might optimize to just "A" to save space. The former is more explicit and simplifies decoding logic, while the latter requires a more complex parser but is more compact. Discussing this trade-off demonstrates an understanding of practical implementation details.

    ⚡
    Pro Tip

    Pro-Move: When RLE helps vs hurts. Red Flag: Not handling single-char runs.

    Want all answers as a PDF for offline study?
    Seven focused volumes with 750+ in-depth answers — Answer Vault →

    Related Python/Coding Questions

    easyWhat are traits in Scala, and how are they different from classes?FreemediumWrite a Python function to check if a string is a palindrome.FreeeasyWhat is the difference between a list and a tuple in Python?FreeeasyExplain the difference between shallow copy and deep copy in Python.FreeeasyWrite a Python function to find the first non-repeating character in a string.Free

    Level up your prep

    Recommended
    Educative
    Educative Unlimited

    800+ hands-on courses — Grokking System Design, Coding Patterns, and AI mock interviews for your DE loop.

    Start learning →

    Some links below are affiliate links. If you buy through them we may earn a small commission at no extra cost to you — it helps keep DataEngPrep free.

    According to DataEngPrep.tech, this is one of the most frequently asked Python/Coding interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.

    ← Back to all questionsMore Python/Coding questions →
    Categories
    All QuestionsSQLSpark / Big DataPython / CodingSystem DesignCloud / ToolsBehavioral
    By Company
    AmazonGoogleDatabricksSnowflakeAWSAzureMicrosoftNetflixUberTCS
    Interview Guides
    All GuidesTop SQL QuestionsTop Spark QuestionsPySpark QuestionsTop Python QuestionsTop System DesignKafka QuestionsAirflow QuestionsSQL Window FunctionsETL QuestionsData Modeling
    Products
    AI Interview CoachAnswer AnalyzerSQL PlaygroundResume AnalyzerAnswer Vault PDFsPricing
    Company
    About & Editorial PolicyContact UsAI DisclosureDisclaimerTerms of ServicePrivacy Policy
    © 2026 DataEngPrep.tech. All rights reserved.
    AboutBlogContactDisclaimer