Magicsheet logo

String Without AAA or BBB

Medium
100%
Updated 6/1/2025

Asked by 3 Companies

String Without AAA or BBB

What is this problem about?

In the "String Without AAA or BBB" problem, you are given two integers, A and B, representing the number of 'a' and 'b' characters you must use. Your goal is to construct a string that contains exactly A 'a's and B 'b's such that no three consecutive characters are the same (i.e., no "aaa" and no "bbb"). Any valid string that meets these criteria is an acceptable answer. It is a classic construction problem that requires balancing the available resources (characters) while following strict placement rules.

Why is this asked in interviews?

Companies like Zalando, Amazon, and Google use this problem to evaluate a candidate's greedy logic and ability to handle state during string construction. It's a test of how you manage constraints. When you have more of one character than the other, you must use the "abundant" character more frequently without violating the "no three in a row" rule. It assesses whether a candidate can write a robust loop that adjusts its strategy based on the remaining counts of 'a' and 'b'.

Algorithmic pattern used

The problem is solved using a Greedy Algorithmic Pattern. At each step, you decide whether to append 'a' or 'b' based on two factors: the current counts of A and B, and the last two characters added to the string. If A > B, you prefer to add 'a', unless the last two characters were already 'aa', in which case you must add 'b'. This greedy choice ensures that you exhaust the more frequent character as quickly as possible without creating a forbidden sequence.

Example explanation (use your own example)

Let's say A = 4 and B = 1.

  1. We have more 'a's, so we add 'a'. String: "a". (A=3, B=1)
  2. We still have more 'a's and the last two aren't "aa", so we add 'a'. String: "aa". (A=2, B=1)
  3. Now the last two are "aa", so we must add 'b'. String: "aab". (A=2, B=0)
  4. We have 'a's left. String: "aaba". (A=1, B=0)
  5. We have one 'a' left. String: "aabaa". (A=0, B=0) Final string "aabaa" contains no "aaa" or "bbb".

Common mistakes candidates make

One common error is not checking the last two characters before adding the next one, leading to "aaa" or "bbb" sequences. Another mistake is using a simple alternating "abab" pattern which fails when one count is much larger than the other (e.g., A=10, B=2). Some candidates also struggle with the loop termination condition or fail to handle cases where the inputs make it impossible to avoid triple characters (though the problem usually guarantees a valid solution exists for the given inputs).

Interview preparation tip

When practicing the String Without AAA or BBB coding problem, try to think of it as a "balancing act." Always prioritize the character with the higher remaining count, but let the "no three in a row" rule act as a hard override. This pattern of "Greedy with Constraints" is very common in interview questions. Make sure your code is clean and handles the case where A == B gracefully.

Similar Questions