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.

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".

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.

Loading coding console...