Longest Border-Ending Diagonal Pattern
Updated
Problem statement
You are given a grid of integers matrix. Find the longest diagonal line of cells whose values follow the pattern 1, 2, 0, 2, 0, ..., which goes on forever.
A valid segment:
- starts at any cell whose value is
1; - keeps going in one of the four diagonal directions, without turning;
- has
2right after the first1, then alternates0and2; - ends at a cell on the edge of the grid: the first row, last row, first column or last column.
Return the length of the longest valid segment. If there is no valid segment, return zero.
Function
longestBorderDiagonal(matrix: int[][]) → intExamples
Example 1
matrix = [[0,0,1,2],[0,2,2,2],[2,1,0,1]]return = 3Start at matrix[2][3] and move up and to the left. The values are 1, 2, 0, at cells (2,3), (1,2) and (0,1). The last cell is in the first row, so this segment is valid, and its length is 3. No valid segment is longer.
Constraints
matrixis not empty, and every row has the same length.- Every value is
0,1or2. - Each step of a segment moves one row and one column, and the direction never changes.
- A solution with time complexity no worse than
O(matrix.length^2 * matrix[0].length^2)fits the time limit of the original test.
More ZipRecruiter Online Assessment questions
- Four-Level Banking System with Scheduled PaymentsOA · Seen Oct 2026
- Count Three-Digit Numbers with Distinct DigitsOA · Seen Oct 2024
- Longest Frequency-Bounded SubarrayOA · Seen Oct 2024
- Minimum Changes to Draw a YOA · Seen Oct 2024
- Apply Binary State OperationsOA · Seen Sep 2024
- Simulate Orchard Rot SpreadOA · Seen Sep 2024
- Compare Odd- and Even-Index SumsOA · Seen Sep 2024
- Coordinate Removals with Column GravityOA · Seen Sep 2024
- Lexicographically Smallest End ReversalOA · Seen Sep 2024
- Recursively Expanded Shell Command CountsOA · Seen Sep 2024
Similar questions at other companies
- Trace Every Water Drop to Its Resting DestinationGoogle · ONSITE INTERVIEWGoogle interview questions
- Robot Survival ProbabilityInMobi · ONSITE INTERVIEWInMobi interview questions
- Rotate Matrix Regions Around the DiagonalsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Grid Paths with Override PassesIMC Trading · OAIMC Trading interview questions
- Rotate Matrix Regions Around the DiagonalsHudson River Trading · OAHudson River Trading interview questions
- Robot Navigation Around LasersRoblox · OARoblox interview questions
- Leaves Remaining After Wind GustsOpenAI · OAOpenAI interview questions
- Apply a Reversi MoveAmazon · OA
- Flip, Invert, and Smooth a Binary ImageAbridge · OAAbridge interview questions
- Count Straight-Line Word Occurrences in a GridOptiver · OAOptiver interview questions