Color Map Boundary Segments So No Three Consecutive Ones Match
Last updated: November 29, 2026
Quick Overview
A Maps rendering style paints n consecutive boundary segments using k colors, and no three consecutive segments may share a color. Count the valid colorings.
0
0
0 solved
A Maps rendering style paints n consecutive boundary segments using k colors, and no three consecutive segments may share a color. Count the valid colorings.
Paint fence is a Google screen staple because the right state (does the last post match the one before it) is not obvious. Interviewers evaluate whether you define states carefully and derive the recurrence instead of guessing it.
What the Interviewer Expects
- Split counts by whether the last two posts match
- Derive transitions with the factor k - 1
- Give base cases for n = 1 and n = 2
- Use O(1) space
- Verify on a small case by enumeration
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
- No four in a row instead of three.
- Adjacent posts must always differ.
- n is 10^18. How do you compute the count modulo a prime?
- Some posts have a fixed color already.
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: integers n >= 0 and k >= 1. Output the number of color sequences of length n over k colors with no three consecutive equal colors. Return 0 for n = 0 by convention (confirm with the interviewer...
High-Level Design
Let same be the count of valid colorings of the first i posts where posts i and i - 1 match, and diff where they differ. Post i + 1 can match post i only if posts i and i - 1 differ, otherwise tha...