FastPrepLargest Component in Three Rows
Problems/Largest Component in Three Rows

Largest Component in Three Rows

Google logoGoogle● HardNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The author described a very similar interview question, not the exact statement. We kept the three-row shape and the constant-space need. Here a component is a group of ones, with a linear-time target and clear limits on the number of columns. Our version is about 90% the same as the reported question.

You are given a binary matrix matrix with exactly three rows and n columns. A component is a maximal group of cells containing 1 connected through horizontal or vertical neighbors.

Return the number of cells in the largest component, or 0 when there are no ones. Diagonal contact does not connect cells.

Your final solution must run in O(n) time using O(1) auxiliary space. The supplied matrix is excluded from the auxiliary-space count.

Function

largestThreeRowComponent(matrix: int[][]) → int

Examples

Example 1

matrix = [[1,1,1],[0,0,1],[1,1,1]]return = 7
matrix
012011110012111

The final column joins the upper and lower runs into one component of seven cells.

Example 2

matrix = [[1,1,1],[1,0,1],[1,1,1]]return = 8
matrix
012011111012111

The middle column separates the frontier into two tails of the same earlier component. Rejoining them must count the old cells only once.

Example 3

matrix = [[0,0],[0,0],[0,0]]return = 0
matrix
01000100200

Components contain only ones, so an all-zero matrix returns 0.

Constraints

  • matrix.length == 3.
  • 1 <= n <= 100000, and every row has length n.
  • Every cell is 0 or 1.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Rectangular K-in-a-RowDatabricks · ONSITE INTERVIEWDatabricks interview questions
  • Count Islands from Streaming RowsAmazon · ONSITE INTERVIEW
  • Making a Large IslandMeta · ONSITE INTERVIEWMeta interview questions
  • Number of IslandsFleetx · ONSITE INTERVIEW
  • Number of IslandsIntuit · ONSITE INTERVIEWIntuit interview questions
  • Number of IslandsWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
  • Connected GroupsFivetran · ONSITE INTERVIEWFivetran interview questions
  • Number of IslandsExpedia · ONSITE INTERVIEWExpedia interview questions
  • Dynamic Island CountsUber · PHONE SCREENUber interview questions
  • Number of Islands IISpaceX · PHONE SCREENSpaceX interview questions

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
  • Google L4 SWE Phone Screen: 1 Experience
  • Google ML Engineering Manager Onsite: 1 Experience
  • Google Experienced SWE First Round and Onsite: 1 Experience
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int largestThreeRowComponent(int[][] matrix) {
        // Write your code here
    }
}
Checking account…