PracHub
QuestionsCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates a candidate's ability to use a stack-based approach to repeatedly cancel out adjacent equal elements in a string. It tests understanding of amortized linear-time string processing and how local reductions can cascade into new matches, a common pattern in coding interviews. The problem is a practical, implementation-focused algorithms exercise rather than a purely conceptual one.

  • medium
  • Omnissa
  • Coding & Algorithms
  • Software Engineer

Repeatedly Remove Adjacent Equal Characters

Company: Omnissa

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Repeatedly Remove Adjacent Equal Characters You are given a string `s` consisting of lowercase English letters. Repeatedly apply the following operation until it can no longer be performed: > Choose any two **adjacent** characters that are equal, and delete **both** of > them. Deleting a pair can bring two previously separated characters next to each other, forming a new adjacent equal pair that must then also be removed. Keep removing until no two adjacent characters in the string are equal, and return the final resulting string. The final result is unique: it does not depend on the order in which you choose the pairs to remove. The result may be the empty string. ### Examples - `s = "abbaca"` -> `"ca"` - Remove the adjacent `"bb"` -> `"aaca"`; now remove the adjacent `"aa"` -> `"ca"`. No adjacent equal pair remains. - `s = "azxxzy"` -> `"ay"` - Remove `"xx"` -> `"azzy"`; remove `"zz"` -> `"ay"`. - `s = "aaaa"` -> `""` - Remove `"aa"` -> `"aa"`; remove `"aa"` -> `""`. - `s = "abc"` -> `"abc"` (no adjacent equal pair exists at the start). ### Constraints - `1 <= s.length <= 100000` - `s` consists only of lowercase English letters (`'a'`-`'z'`). - Return the final string after all possible removals; it may be empty. - Aim for a single linear pass over the input.

Quick Answer: This question evaluates a candidate's ability to use a stack-based approach to repeatedly cancel out adjacent equal elements in a string. It tests understanding of amortized linear-time string processing and how local reductions can cascade into new matches, a common pattern in coding interviews. The problem is a practical, implementation-focused algorithms exercise rather than a purely conceptual one.

You are given a string `s` of lowercase English letters. Repeatedly choose any two **adjacent** characters that are equal and delete **both** of them. Deleting a pair can bring two previously separated characters next to each other, forming a new adjacent equal pair that must then also be removed. Keep going until no two adjacent characters are equal, and return the final string (which may be empty). The final result is unique regardless of the order of removals. Examples: - `s = "abbaca"` -> `"ca"` (remove `bb` -> `aaca`, then `aa` -> `ca`) - `s = "azxxzy"` -> `"ay"` (remove `xx` -> `azzy`, then `zz` -> `ay`) - `s = "aaaa"` -> `""` - `s = "abc"` -> `"abc"` Aim for a single linear pass.

Constraints

  • 1 <= s.length <= 100000
  • s consists only of lowercase English letters ('a'-'z').
  • Return the final string after all possible removals; it may be empty.
  • Aim for a single linear pass over the input.

Examples

Input: ("abbaca",)

Expected Output: "ca"

Explanation: Remove adjacent 'bb' -> 'aaca', then 'aa' -> 'ca'. No adjacent equal pair remains.

Input: ("azxxzy",)

Expected Output: "ay"

Explanation: Remove 'xx' -> 'azzy', then 'zz' -> 'ay'.

Input: ("aaaa",)

Expected Output: ""

Explanation: Two pairs of 'aa' cancel completely, leaving the empty string.

Input: ("abc",)

Expected Output: "abc"

Explanation: No adjacent equal pair exists, so the string is unchanged.

Input: ("a",)

Expected Output: "a"

Explanation: A single character has no adjacent pair and is returned as-is.

Input: ("aabccba",)

Expected Output: "a"

Explanation: 'aa' cancels -> 'bccba'; 'cc' cancels -> 'bba'; 'bb' cancels -> 'a'.

Input: ("mississippi",)

Expected Output: "m"

Explanation: Repeated cascading cancellations of 'ss', 'ii', and 'pp' collapse everything except the leading 'm'.

Hints

  1. Process the string left to right and keep the characters that survive so far in a stack.
  2. For each new character, if it equals the character on top of the stack, they cancel out: pop the top instead of pushing. Otherwise push the new character.
  3. The stack naturally handles cascading removals: after a pop, the newly exposed top can immediately cancel with the next character. Join the stack at the end for the answer.
Last updated: Jul 1, 2026

Related Coding Questions

  • Alternate Odd and Even Numbers with Two Threads - Omnissa (medium)

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
  • AI Coding 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.