Reported February 2024
Snowflakedynamic programming

Count Ways to Color Houses

Reported by candidates from Snowflake's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Snowflake OA. Under 2s to a working solution.
Founder's read

Snowflake's February OA included a house-coloring problem, and it's a textbook dynamic programming trap. You're given a set of houses and colors, and you need to count valid colorings under some constraint (usually adjacent houses can't share a color). The naive approach times out. Most candidates freeze on the state definition. StealthCoder reads the constraint, spots the DP recurrence, and hands you the solution in seconds so you don't waste 20 minutes on a wrong greedy attempt.

Pattern and pitfall

This is a classic DP problem where you build up valid colorings house by house. The state is typically dp[i][c] = number of ways to color houses 0 to i where house i has color c. The transition counts valid color assignments for the next house based on what the current house was colored. The pitfall is overcomplicating the constraint parsing or mishandling the base case. Once you nail the state and recurrence, it's a straightforward O(n * k^2) solution where n is the number of houses and k is the number of colors. StealthCoder's real value here is reading the exact constraint off screen and building the table while you focus on not blanking under pressure.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Count Ways to Color Houses cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as paint house ii. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Snowflake's OA.

Snowflake reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Ways to Color Houses FAQ

Is this problem just a variation of House Robber II?+

No. House Robber is about maximizing value. This is about counting all valid colorings under adjacency constraints. The DP structure is similar, but the logic and goal are different. Read the problem carefully.

What if there are more than 2 colors?+

The recurrence generalizes. You track dp[i][c] for each color c, and for each next house, you sum over all colors that are not the color of the current house. The complexity grows linearly with the number of colors, not exponentially.

Can I solve this without DP?+

Greedy won't work because you need to count all valid configurations, not find one. Backtracking will time out on large inputs. DP is the intended solution and runs in polynomial time.

How do I handle the first house?+

Initialize dp[0][c] = 1 for all colors c. Every color is valid for the first house. Then iterate from house 1 onward, applying the adjacency constraint.

What if the problem adds a third constraint, like certain houses must be certain colors?+

Fix the dp state for those houses to 0 or 1 at initialization, then run the standard recurrence. The constraint integrates cleanly into the DP table.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Snowflake.

OA at Snowflake?
Invisible during screen share
Get it