cswork / libraryOpen practice workspace

Coding questions · Meta

Remove adjacent duplicate groups repeatedly

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

What a Strong Answer Covers

  • A clear algorithm with the right data structures and enough pseudocode or code-level detail to implement it.
  • A correctness argument that explains why the algorithm covers all required cases.
  • Time and space complexity, plus at least one alternative approach when relevant.
  • Focused tests for normal cases, edge cases, and failure modes.

Follow-up Questions

  • How would the approach change if the input were streaming or too large for memory?
  • What invariants would you assert in production code?
  • Which tests would catch off-by-one, duplicate, or tie-breaking bugs?

Remove Adjacent Duplicate Groups Repeatedly

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

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.

Solution

Solution Alignment

The prompt asks for an implementation-level answer. The safest way to present it is to define the state, maintain clear invariants, then walk through complexity and tests.

Problem Restatement

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.

Recommended Approach

Use the string constraints to choose between two pointers, a stack, frequency counts, prefix/suffix state, or dynamic programming. Maintain the invariant that processed characters have already been normalized, counted, or matched according to the operation.

Correctness

The implementation should maintain an invariant after each loop or operation that directly matches the problem statement. At termination, that invariant implies the returned value has considered every valid candidate exactly once, or has preserved the required data-structure state after every API call.

Complexity

Most direct string scans are O(n) time. Space ranges from O(1) for two pointers to O(n) for stacks, maps, or DP tables.

Edge Cases and Tests

Empty string, length 1, repeated characters, invalid characters, case sensitivity, Unicode vs ASCII, and very long input.