PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

Remove adjacent duplicate groups repeatedly evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

  • Medium
  • Meta
  • Coding & Algorithms
  • Software Engineer

Remove adjacent duplicate groups repeatedly

Company: Meta

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: Medium

Interview Round: Technical Screen

Given a string s, repeatedly delete any maximal contiguous group of identical characters whose length is at least 2. After each deletion, the remaining parts concatenate and the process continues until no such group exists. Return the final string. Example: s = "abbba" → delete "bbb" → "aa" → delete "aa" → "". Design an O(n) time algorithm with O(n) extra space (e.g., using a stack-like technique), explain correctness, and analyze time/space complexity. Follow-up: generalize to delete groups of length ≥ k for a given k ≥ 2 while maintaining near-linear performance.

Quick Answer: Remove adjacent duplicate groups repeatedly evaluates algorithm design, data structures, correctness, complexity, edge cases, and implementation details in a realistic interview setting. A strong answer states assumptions, handles edge cases, explains trade-offs, and shows how to validate the result clearly.

Given a string `s`, repeatedly delete any maximal contiguous group of identical characters whose length is at least 2. After each deletion the remaining parts concatenate and the process continues until no such group exists. Return the final string. A deletion can cause the characters on either side of the removed group to become adjacent, possibly forming a new group of length >= 2 that is then itself eligible for deletion. Keep deleting until the string is stable. Example: `s = "abbba"` -> delete `"bbb"` -> `"aa"` -> delete `"aa"` -> `""`, so the answer is the empty string. Implement `removeGroups(s)` (Python: `solution(s)`) running in O(n) time and O(n) extra space using a stack-like technique.

Constraints

  • 0 <= len(s) <= 10^5
  • s consists of printable characters (lowercase English letters in the examples)
  • Deletions cascade: removing a group can create a new deletable group from the now-adjacent neighbours
  • Return the empty string if everything is deleted

Examples

Input: ("abbba",)

Expected Output: ""

Explanation: Delete "bbb" -> "aa", then delete "aa" -> empty string.

Input: ("aabccba",)

Expected Output: "a"

Explanation: Delete "aa" -> "bccba", delete "cc" -> "bba", delete "bb" -> "a".

Input: ("deeedbbcccbdaa",)

Expected Output: "bd"

Explanation: Delete eee -> ddbbcccbdaa, delete dd -> bbcccbdaa, delete bb -> cccbdaa, delete ccc -> bdaa, delete aa -> bd; no group of length >= 2 remains.

Input: ("",)

Expected Output: ""

Explanation: Empty input stays empty.

Input: ("a",)

Expected Output: "a"

Explanation: A single character has no group of length >= 2, so it survives.

Input: ("abcde",)

Expected Output: "abcde"

Explanation: All runs have length 1; nothing is deleted.

Input: ("aaaa",)

Expected Output: ""

Explanation: One maximal group of length 4 (>= 2) is deleted entirely.

Input: ("aaabbbaaa",)

Expected Output: ""

Explanation: Delete aaa -> bbbaaa, delete bbb -> aaa, delete aaa -> empty: cascading deletions clear the whole string.

Hints

  1. Maintain a stack of (character, run-length) pairs as you scan left to right, accumulating consecutive equal characters into the top entry.
  2. A run is only ever deletable once you know it is complete. Finalize the top run when a different character arrives (or at end of string): if its length is >= 2, pop it.
  3. Popping a >= 2 run exposes the previous survivor, which may equal the incoming character and merge with it — keep checking the new top against the incoming char so deletions cascade correctly.
  4. After the scan, the final top run and any merges it triggers must also be collapsed; every group below the top is guaranteed to have length 1, so collapsing only ever happens at the top.
Last updated: Jun 26, 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

  • Find Shortest Unique Prefixes - Meta (medium)
  • Compute Exclusive Execution Times - Meta (medium)
  • Solve Tree Columns And Maze Variants - Meta (medium)
  • Solve Tree Diameter and Palindromic Counts - Meta (medium)
  • Simulate Monster Team Battles - Meta (hard)