PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in string manipulation, algorithmic thinking, and efficient use of data structures for handling contiguous character groups and iterative reductions.

  • medium
  • Attentive
  • Coding & Algorithms
  • Software Engineer

Remove Repeated Character Groups

Company: Attentive

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given a string `s` and an integer `k`. Repeatedly remove any contiguous group of `k` identical characters. After a removal, the remaining parts of the string are concatenated, which may create new removable groups. Continue until no more removals are possible, and return the final string. As a warm-up, explain how you would solve the special case `k = 3`, then generalize the approach to any `k`. Constraints: - `1 <= len(s) <= 100000` - `2 <= k <= 100000` - `s` contains lowercase English letters Example: - Input: `s = "abbbaaac"`, `k = 3` - Output: `"c"`

Quick Answer: This question evaluates proficiency in string manipulation, algorithmic thinking, and efficient use of data structures for handling contiguous character groups and iterative reductions.

You are given a lowercase string `s` and an integer `k`. Repeatedly remove any contiguous group of **at least** `k` identical characters. After a removal, the remaining parts of the string are concatenated, which may create new removable groups. Continue until no more removals are possible, and return the final string. Warm-up: first think about the special case `k = 3`. If a middle group disappears, two groups on its left and right may become adjacent and merge into a new removable group. Then generalize the same idea to any `k`. Example: `s = "abbbaaac"`, `k = 3` - Remove `bbb` -> `aaaa c` - Now `aaaa` is a group of length 4, which is at least 3, so remove it - Final result: `"c"`

Constraints

  • 1 <= len(s) <= 100000
  • 2 <= k <= 100000
  • s contains only lowercase English letters

Examples

Input: ("abbbaaac", 3)

Expected Output: "c"

Explanation: Remove `bbb` first, producing `aaaac`. The `aaaa` run has length 4, which is at least 3, so it is also removed. Only `c` remains.

Input: ("a", 2)

Expected Output: "a"

Explanation: A single character cannot form a removable group.

Input: ("deeedbbcccbdaa", 3)

Expected Output: "aa"

Explanation: `eee` is removed, then the two `d` runs merge later through cascading deletions. After all possible removals, only `aa` remains.

Input: ("aaaa", 3)

Expected Output: ""

Explanation: The entire run `aaaa` has length 4, which is at least 3, so the whole string is removed.

Input: ("abccba", 2)

Expected Output: ""

Explanation: Remove `cc` -> `abba`, then remove `bb` -> `aa`, then remove `aa` -> empty string.

Input: ("baaab", 3)

Expected Output: "bb"

Explanation: Remove `aaa`, and the two remaining `b` characters become adjacent. Since their merged length is 2, which is less than 3, they stay.

Input: ("abc", 5)

Expected Output: "abc"

Explanation: No group has length at least 5, so nothing is removed.

Hints

  1. For the warm-up `k = 3`, think in terms of runs of equal characters rather than individual characters. Removing one run can cause neighboring runs to merge.
  2. A stack of `[character, count]` pairs lets you track the compressed string built so far. When a merged run reaches length at least `k`, delete that whole run.
Last updated: May 7, 2026

Related Coding Questions

  • Implement Due Broadcast Sender - Attentive (medium)

Loading coding console...

PracHub

Master your tech interviews with 8,500+ 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.