Count Colorings of a Three-Lane Map Strip With No Matching Neighbors
Last updated: November 29, 2026
Quick Overview
A Maps style test paints an n by 3 grid of road tiles with three colors so that no two edge-adjacent tiles share a color. Count the valid colorings modulo 1,000,000,007.
0
0
0 solved
A Maps style test paints an n by 3 grid of road tiles with three colors so that no two edge-adjacent tiles share a color. Count the valid colorings modulo 1,000,000,007.
A Google onsite hard that looks 2D but becomes a 1D DP over rows once you classify row patterns. Interviewers look for the insight that only two pattern types matter and a derivation of the transition counts, not memorized constants.
What the Interviewer Expects
- Enumerate valid single-row patterns (12 total)
- Group them into two types: ABA and ABC
- Derive how many next-row patterns of each type fit above each type
- Roll two counters over n rows
- Verify transitions by brute force
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
- Use k colors instead of three.
- The strip is n by 4. How does the approach change?
- Some tiles are pre-colored.
- n is 10^18.
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: integer n >= 1. Count colorings of an n x 3 grid using 3 colors where horizontally and vertically adjacent cells differ, mod . For n = 1 the answer is 12: 6 patterns like ABA and 6 like...
High-Level Design
Each row is a 3-cell pattern with adjacent cells different. Type ABA (outer cells match) has 6 patterns; type ABC (all different) has 6. Whether a next row is compatible depends only on the current ro...