Build a string with exact counts of a and b and no three identical letters in a row

Quick Overview

Given counts of the letters a and b, build any string that uses exactly that many of each and never contains three identical letters in a row. It tests greedy construction, an argument for why the choice rule never gets stuck, and care with edge inputs such as a zero count or a wide gap between the counts.

Build a string with exact counts of a and b and no three identical letters in a row

Company: Grab

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given two integers `a` and `b`, return any string `s` that meets all of these conditions: - `s` has length `a + b` and contains exactly `a` letters `'a'` and exactly `b` letters `'b'`. - The substring `"aaa"` does not occur in `s`. - The substring `"bbb"` does not occur in `s`. Any string that meets the conditions is accepted. ```text Input: a = 1, b = 2 Output: "bab" ``` "bab" has length 3, one 'a' and two 'b's, and no three equal letters in a row. "abb" and "bba" are also accepted. ```text Input: a = 4, b = 1 Output: "aabaa" ``` The single 'b' has to split the four 'a's into two pairs, so "aabaa" is the only valid answer here. ```hint Which letter to spend first The letter with more copies left is the one that can run out of legal places. Ask what happens if you spend it too slowly early on. ``` ```hint What one step can break The only illegal move is a third equal letter in a row, so each decision needs only a small part of the string built so far. ``` ### Constraints & Assumptions - `0 <= a, b <= 100` - The input always has at least one valid answer. ### What a Strong Answer Covers - A rule that picks each next letter from the remaining counts and the end of the string built so far. - Why that rule never gets stuck on an input that is guaranteed to have an answer. - The condition on `a` and `b` under which an answer exists. - Edge inputs: a zero count, equal counts, and the widest allowed gap between the counts. - Time and space complexity. ### Follow-up Questions - If the input could have no valid answer, how would you detect that before building anything? - How would you return the lexicographically smallest valid string instead of any valid one? - How does the approach change for three letters `a`, `b` and `c` under the same rule against three equal letters in a row?

Overview: Given counts of the letters a and b, build any string that uses exactly that many of each and never contains three identical letters in a row. It tests greedy construction, an argument for why the choice rule never gets stuck, and care with edge inputs such as a zero count or a wide gap between the counts.

Grab logo
Grab
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given two integers a and b, return any string s that meets all of these conditions:

  • s has length a + b and contains exactly a letters 'a' and exactly b letters 'b' .
  • The substring "aaa" does not occur in s .
  • The substring "bbb" does not occur in s .

Any string that meets the conditions is accepted.

Input: a = 1, b = 2
Output: "bab"

"bab" has length 3, one 'a' and two 'b's, and no three equal letters in a row. "abb" and "bba" are also accepted.

Input: a = 4, b = 1
Output: "aabaa"

The single 'b' has to split the four 'a's into two pairs, so "aabaa" is the only valid answer here.

Constraints & Assumptions

  • 0 <= a, b <= 100
  • The input always has at least one valid answer.

What a Strong Answer Covers Guidance

  • A rule that picks each next letter from the remaining counts and the end of the string built so far.
  • Why that rule never gets stuck on an input that is guaranteed to have an answer.
  • The condition on a and b under which an answer exists.
  • Edge inputs: a zero count, equal counts, and the widest allowed gap between the counts.
  • Time and space complexity.

Follow-up Questions Guidance

  • If the input could have no valid answer, how would you detect that before building anything?
  • How would you return the lexicographically smallest valid string instead of any valid one?
  • How does the approach change for three letters a , b and c under the same rule against three equal letters in a row?