PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in string manipulation, run-length encoding concepts, and dynamic programming-based optimization under deletion constraints.

  • hard
  • Databricks
  • Coding & Algorithms
  • Software Engineer

Minimize run-length encoded length with deletions

Company: Databricks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

### Problem You are given a string `s` (uppercase English letters) and an integer `k`. You may delete **at most `k` characters** from `s`. After deletions, you compress the resulting string using **run-length encoding (RLE)**: - Consecutive equal characters are replaced by the character followed by the run length **only if the run length > 1**. - Examples: - `"A" -> "A"` - `"AA" -> "A2"` - `"AAA" -> "A3"` - `"AABCCC" -> "A2BC3"` ### Task Return the **minimum possible length** of the RLE-compressed string after deleting at most `k` characters. ### Input / Output - Input: string `s`, integer `k` - Output: integer (minimum compressed length) ### Constraints (typical interview bounds) - `1 <= len(s) <= 100` - `0 <= k <= len(s)` ### Clarifications - The “length” is the number of characters in the encoded string (e.g., `"A12"` has length 3).

Quick Answer: This question evaluates proficiency in string manipulation, run-length encoding concepts, and dynamic programming-based optimization under deletion constraints.

You are given a string s consisting of uppercase English letters and an integer k. You may delete at most k characters from s. After deletions, compress the remaining string using run-length encoding (RLE): each maximal block of consecutive equal characters becomes the character followed by its count only when the count is greater than 1. For example, A becomes A, AA becomes A2, and AABCCC becomes A2BC3. Return the minimum possible length of the compressed string. If all characters are deleted, the compressed length is 0.

Constraints

  • 1 <= len(s) <= 100
  • 0 <= k <= len(s)
  • s consists only of uppercase English letters

Examples

Input: ('AAABCCCD', 2)

Expected Output: 4

Explanation: Delete B and D to get AAACCC, which compresses to A3C3 with length 4.

Input: ('AABBA', 2)

Expected Output: 2

Explanation: Delete both B characters to get AAA, which compresses to A3 with length 2.

Input: ('A', 0)

Expected Output: 1

Explanation: No deletions are allowed. A compresses to A, so the length is 1.

Input: ('ABCDE', 5)

Expected Output: 0

Explanation: You may delete all characters, leaving an empty string with compressed length 0.

Input: ('AAAAAAAAAA', 1)

Expected Output: 2

Explanation: Without deletion, AAAAAAAAAA compresses to A10 with length 3. Deleting one A gives A9 with length 2.

Input: ('AAABA', 1)

Expected Output: 2

Explanation: Delete B to merge the runs into AAAA, which compresses to A4 with length 2.

Input: ('AABCCC', 0)

Expected Output: 5

Explanation: With no deletions, AABCCC compresses to A2BC3, whose length is 5.

Hints

  1. Use dynamic programming on suffixes. At each position, either delete the current character or keep it as the start of the next encoded run.
  2. When you keep s[i], try extending its run to the right while deleting non-matching characters. The cost of a run depends only on how many copies you keep.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Choose the Best Travel Mode - Databricks (medium)
  • Implement an Alternating Tic-Tac-Toe Game - Databricks (hard)
  • Implement a Snapshot Set Iterator - Databricks (medium)
  • Find the Best Commute Mode - Databricks (medium)
  • Partition a Target String by Source Substrings - Databricks (medium)