Longest Test String Without Three Identical Letters in a Row
Last updated: November 29, 2026
Quick Overview
Given at most a copies of 'a', b copies of 'b' and c copies of 'c', build the longest possible string that never contains three identical letters consecutively.
0
0
0 solved
Given at most a copies of 'a', b copies of 'b' and c copies of 'c', build the longest possible string that never contains three identical letters consecutively.
A Google screen greedy question framed as generating fuzzing inputs for a parser that chokes on triple repeats. The interviewer checks a max-heap greedy that falls back to the second-largest letter when the largest would create a triple.
What the Interviewer Expects
- Uses a max-heap of remaining counts
- Checks the last two output characters before placing
- Falls back to the next letter when blocked
- Stops when no letter can be placed
- Explains why the greedy maximizes length
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- Generalize to a limit of k in a row
- Generalize to an arbitrary alphabet
- Return the lexicographically smallest longest string
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: non-negative integers a, b, c.
- Output: a longest string using at most those counts, with no substring 'aaa', 'bbb' or 'ccc'.
- Any longest valid string is accepted....
High-Level Design
Spend the most plentiful letter whenever possible, since it is the one at risk of being stranded. Keep a max-heap of (-count, letter). Pop the largest. If the last two characters already equal it, p...