FastPrepLongest Consecutive Same-Color Line
Problems/Longest Consecutive Same-Color Line

Longest Consecutive Same-Color Line

Google logoGoogle● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: The report provides the color-grid objective and one exact example, but omits the callable interface, bounds, empty-grid behavior, and an explicit list of both diagonal orientations. The judged core task matches the visible source at about 94%.

You are given a non-empty rectangular grid of uppercase color symbols. The grid is represented by grid, where grid[row][column] is the color at that cell.

A same-color line is a contiguous sequence of cells that all contain the same symbol. A line may extend in exactly one of these four orientations:

  • horizontally from left to right,
  • vertically from top to bottom,
  • diagonally down and right, or
  • diagonally down and left.

Return the maximum number of cells in any same-color line.

Function

longestSameColorLine(grid: String[]) → int

Examples

Example 1

grid = ["RRRG","RRBB","GRRR","BGBR"]return = 4

The cells from (0, 0) through (3, 3) form a down-right diagonal of four R symbols. No longer line exists.

Example 2

grid = ["RRGGGG"]return = 4

The only row ends with four consecutive G symbols, so the longest line has length 4.

Example 3

grid = ["R","G","G","G"]return = 3

The single column contains a vertical run of three consecutive G symbols.

Constraints

  • 1 <= grid.length <= 1000.
  • 1 <= grid[i].length <= 1000.
  • Every row has the same length.
  • The total number of cells is at most 100000.
  • Every cell contains one uppercase English letter from A through Z.

More Google problems

  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int longestSameColorLine(String[] grid) {
        // Write your code here
    }
}
Checking account…