FastPrepLongest Border-Ending Diagonal Pattern
Problems/Longest Border-Ending Diagonal Pattern

Longest Border-Ending Diagonal Pattern

ZipRecruiter logoZipRecruiter● MediumNEW GRADOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in three ZipRecruiter new-grad online assessments. All three keep the 1,2,0,2 pattern along a diagonal, the four diagonal directions and the goal of finding the longest line. The two fuller reports also say the line must end on the edge of the grid. None of them gives input limits or a function signature, and their screenshots were not kept.

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 2 right after the first 1, then alternates 0 and 2;
  • 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[][]) → int

Examples

Example 1

matrix = [[0,0,1,2],[0,2,2,2],[2,1,0,1]]return = 3
matrix
0123000121022222101

Start 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

  • matrix is not empty, and every row has the same length.
  • Every value is 0, 1 or 2.
  • 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

ZipRecruiter interview experiences

  • ZipRecruiter SWE OA, Level Not Reported: 23 Experiences
  • ZipRecruiter SWE Intern OA Scores: 4 Experiences
  • ZipRecruiter SWE New Grad OA: 9 Experiences
All ZipRecruiter interview questionsSee ZipRecruiter hiring insights
Saved
class Solution {
    public int longestBorderDiagonal(int[][] matrix) {
        // write your code here
    }
}
Checking account…