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.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

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
1D dynamic programming
Profile DP
Pattern classification
Graph coloring
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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 109+710^9+7. 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...


Submit Your Answer
Markdown supported