PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCareers
|Home/Coding & Algorithms/Google

Count same-color squares in a character grid

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of 2D array manipulation, spatial pattern detection, and algorithmic counting with attention to time and space complexity.

  • hard
  • Google
  • Coding & Algorithms
  • Software Engineer

Count same-color squares in a character grid

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given a 2D grid (matrix) of characters. Each character represents a color: cells with the same character are considered the same color. Formally: - The grid has `m` rows and `n` columns. - `grid[i][j]` is an alphabetic character (e.g., `'a'`–`'z'` or `'A'`–`'Z'`). A **square** in this grid is defined as a contiguous `k × k` submatrix aligned with the grid axes, for some integer `k ≥ 1`. A square is **monochromatic** if **all** of its cells contain the **same** character (i.e., the same color). Task: - Count and return the total number of monochromatic squares in the grid. - Count all sizes of squares (1×1, 2×2, ..., up to the largest possible that fits in the grid). Example (just for clarity, not necessarily exhaustive): If the grid is: ``` a a b a a b b b b ``` Then some monochromatic squares include: - All 1×1 cells (each single cell is a monochromatic square). - The 2×2 square in the top-left corner formed by `'a'`. Your function should return the total count of such monochromatic squares for a given input grid.

Quick Answer: This question evaluates understanding of 2D array manipulation, spatial pattern detection, and algorithmic counting with attention to time and space complexity.

Related Interview Questions

  • Compute Turnstile Crossing Times - Google (hard)
  • Simulate In-Place Cellular State Updates - Google (hard)
  • Determine Whether a Word Exists in a Graph - Google (medium)
  • Solve Shortest Paths and Rental Allocation - Google (medium)
  • Solve Two Array Optimization Problems - Google (medium)
Google logo
Google
Nov 22, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
6
0

You are given a 2D grid (matrix) of characters. Each character represents a color: cells with the same character are considered the same color.

Formally:

  • The grid has m rows and n columns.
  • grid[i][j] is an alphabetic character (e.g., 'a' – 'z' or 'A' – 'Z' ).

A square in this grid is defined as a contiguous k × k submatrix aligned with the grid axes, for some integer k ≥ 1.

A square is monochromatic if all of its cells contain the same character (i.e., the same color).

Task:

  • Count and return the total number of monochromatic squares in the grid.
  • Count all sizes of squares (1×1, 2×2, ..., up to the largest possible that fits in the grid).

Example (just for clarity, not necessarily exhaustive):

If the grid is:

a a b
a a b
b b b

Then some monochromatic squares include:

  • All 1×1 cells (each single cell is a monochromatic square).
  • The 2×2 square in the top-left corner formed by 'a' .

Your function should return the total count of such monochromatic squares for a given input grid.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Google•More Software Engineer•Google Software Engineer•Google Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • Careers
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.