Magicsheet logo

Longest Happy String

Medium
57.5%
Updated 6/1/2025

Longest Happy String

What is this problem about?

The Longest Happy String interview question asks you to construct the longest possible string using only the characters 'a', 'b', and 'c'. You are given the maximum number of times each character can be used (integers a, b, and c). However, there is a catch: the resulting string cannot contain three consecutive identical characters (e.g., "aaa" is illegal). Your goal is to return a string that follows these rules and is as long as possible.

Why is this asked in interviews?

This is a prominent problem asked by companies like Microsoft and Amazon because it is a pure test of Greedy algorithms and Priority Queues (Heaps). It challenges candidates to think logically about resource depletion and constraints. The interviewer wants to see if you can build a system that prioritizes using the most abundant character while actively avoiding the "three-in-a-row" penalty.

Algorithmic pattern used

This problem perfectly aligns with the Greedy Strategy implemented via a Max Heap (Priority Queue). At any given step, your greedy choice should be to append the character with the highest remaining count to your string. However, if appending that character would create a forbidden sequence (three identical characters in a row), you must instead append the character with the second highest count, effectively breaking the chain, and then put both characters back into the heap for future consideration.

Example explanation

Suppose a = 1, b = 1, c = 7. We put them in a max heap based on counts: [ (7, 'c'), (1, 'b'), (1, 'a') ].

  1. Pop the most frequent: (7, 'c'). Append two 'c's (to maximize usage without breaking the rule). String is "cc". 'c' count is now 5.
  2. We can't use 'c' again right now, otherwise we'd get "ccc". So we pop the next most frequent: (1, 'b'). Append one 'b'. String is "ccb". 'b' count is 0.
  3. Push 'c' back into the heap. Heap is [ (5, 'c'), (1, 'a') ].
  4. Pop 'c' again. Append two 'c's. String is "ccbcc". 'c' count is 3.
  5. Pop 'a'. Append one 'a'. String is "ccbcca". 'a' count is 0.
  6. Push 'c' back. Heap is [ (3, 'c') ].
  7. Pop 'c'. Append two 'c's. String is "ccbccacc". 'c' count is 1.
  8. We only have 'c' left, but appending more would break the rule. We stop. The output "ccbccacc" is the longest happy string.

Common mistakes candidates make

A common error is overcomplicating the greedy choice by trying to look too far ahead, leading to tangled recursion. Another frequent mistake is forgetting to check the last two characters of the dynamically built string before appending a new character. Candidates sometimes just pop from the heap and append blindly, accidentally creating the illegal "aaa" pattern.

Interview preparation tip

When tackling the Longest Happy String coding problem, get comfortable writing custom comparators for Priority Queues. Remember the core loop logic for this pattern: Pop the max element. If it causes a violation, temporarily hold it, pop the second max element, use it once to break the violation, and then push the first element back into the heap. This "hold and swap" technique is crucial for greedy constraint problems.

Similar Questions