← Questions

Repeatedly remove runs of equal adjacent characters

0
MetaCodingLast asked Jan 2026

A coding interview question asked in Meta's AI-enabled coding round, last asked Jan 2026. It tests stacks and strings.

Given a string, delete every run of two or more equal adjacent characters, and keep deleting as new runs form, until no such run remains. Return what is left. For example xyyyx loses yyy, which leaves xx, which is deleted too, so the answer is the empty string. Do it in one pass.

Process the string from left to right. A run is deleted as soon as it is complete (the next character differs, or the string ends). After a deletion the characters on either side become neighbours and may join into a longer run.

Interface

Unless you agree on a different interface with the interviewer, assume:

  • reduce_string(s: str) -> str.

Example

"xyyyx"     ->  ""       # yyy goes, then xx goes
"abbbacca"  ->  "a"      # bbb goes, aa goes, cc goes, one a is left
"aabccbd"   ->  "d"      # aa goes, cc goes, then bb goes
"abbaa"     ->  ""       # bb goes, the a's join into aaa, which goes
"abc"       ->  "abc"

Constraints

  • 0 <= len(s) <= 10^5; lowercase letters.

Clarifying questions

  • Is a run two or more characters, or exactly k for some given k?
  • Are runs deleted one at a time as they complete (as above), or all at once in rounds? The two rules can give different answers: with rounds, abbaa deletes bb and aa together and leaves a.
  • Is the string only lowercase letters?

What a strong answer covers

  • The naive approach, scanning and rebuilding the string until nothing changes, is O(n^2).
  • A stack of (character, count) pairs: extend the top run when the character matches; when it differs, pop the top run if its count is at least 2, then merge the new character with the run now on top if it matches, else push it.
  • Popping the last run at the end of the string if it is at least 2 long.
  • O(n) time and O(n) space.
  • Tests: empty string, no runs, the whole string one run, runs that cascade several times, the example where the two deletion rules differ.

Follow-ups

  • Delete only runs of exactly k equal characters.
  • Delete runs of length at least 3, as in a match-three game.
  • Return how many deletions happened, as well as the final string.

Where it's asked

  • Meta · AI-enabled coding · Last asked Jan 202660 min · Several progressive tasks on a starter codebase in CoderPad with an AI assistant panel; usually two rounds · The first task usually without AI, later tasks with it

What it tests

stacks · strings

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding