Largest Component in Three Rows
Updated
Problem statement
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[][]) → intExamples
Example 1
matrix = [[1,1,1],[0,0,1],[1,1,1]]return = 7The 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 = 8The 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 = 0Components contain only ones, so an all-zero matrix returns 0.
Constraints
matrix.length == 3.1 <= n <= 100000, and every row has lengthn.- Every cell is
0or1.
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