The Maximum Height of a Triangle coding problem gives you two colors of balls (say red and blue) with counts red and blue. You want to build the tallest possible triangle where each row must be filled with balls of a single color, alternating colors between rows, and row k requires exactly k balls. Maximize the number of rows (height) you can form.
Salesforce includes this problem to test greedy thinking and simulation under constraints. While the problem sounds simple, it requires careful enumeration of both starting-color possibilities and correct tracking of ball counts. It is a good warm-up problem for greedy interviews and tests whether candidates can handle state simulation without overcomplicating it.
The solution uses greedy enumeration. Try both starting configurations: start with color A on row 1, or start with color B on row 1. For each configuration, simulate building rows greedily: row k uses k balls of the current color. If you run out of the current color's balls, stop. Track the maximum height across both configurations. Each simulation is O(sqrt(n)) since the total balls used up to row h is h*(h+1)/2.
Red = 4, Blue = 6.
h*(h+1)/2 can overflow 32-bit integers.For the Array Enumeration interview pattern, always enumerate both starting configurations for alternating-constraint problems. Code a clean helper function simulate(start_color) and call it twice. This pattern applies to many interview problems involving alternating sequences.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Count Good Triplets | Easy | Solve | |
| Detect Pattern of Length M Repeated K or More Times | Easy | Solve | |
| Find the Peaks | Easy | Solve | |
| Collecting Chocolates | Medium | Solve | |
| Coordinate With Maximum Network Quality | Medium | Solve |