Largest Diamond of Ones in a Grid
Updated
Problem statement
You are given an m x n grid grid of 0s and 1s.
A diamond of size k (with k >= 0) around a center cell (r, c) is the set of cells (i, j) with |i - r| + |j - c| <= k. In words: every cell you can reach from the center in at most k steps up, down, left or right. A diamond of size 0 is just the center cell.
A diamond counts only if all of its cells are inside the grid and equal to 1. Its diagonal is the line of cells through the center from its top cell to its bottom cell, which has 2k + 1 cells.
Return the diagonal length of the largest diamond that counts, or 0 if the grid has no 1.
Function
largestDiamond(grid: int[][]) → intExamples
Example 1
grid = [[1,0,1,0,0],[0,1,1,1,0],[1,1,1,1,1],[0,1,1,1,1],[0,0,1,0,1]]return = 5Take the center at row 2, column 2 and size 2. The 13 cells whose row distance plus column distance from it is at most 2 are all 1 and inside the grid. Its diagonal runs from (0, 2) down to (4, 2), so it is 5 cells long. A diamond of size 3 would need 7 rows, and the grid has only 5.
Example 2
grid = [[0,1,1,0],[1,1,0,1],[1,0,1,1]]return = 1A diamond of size 1 needs a center whose 4 neighbors (up, down, left, right) are inside the grid and equal to 1. Only (1, 1) and (1, 2) have all 4 neighbors inside the grid. (1, 2) is 0, and (1, 1) has a 0 below it at (2, 1). So the best is a single 1 cell, a diamond of size 0 with diagonal 1.
Example 3
grid = [[0,0,0,0,0],[0,0,0,0,0]]return = 0There is no 1 in the grid, so there is no diamond. The answer is 0.
Constraints
m == grid.lengthandn == grid[i].lengthfor every row1 <= m, n <= 100grid[i][j]is0or1
More Rippling Phone Screen questions
- Filter, Sort, and Deduplicate Scheduled TasksPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Stateful Log Transform and SearchPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Dependency-Aware Task SchedulerPHONE SCREEN · Seen Nov 2026
- Delivery Cost Tracker with Effective-Dated RatesPHONE SCREEN · Seen Nov 2026
- Camel CardsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Best and Worst Camel Card CompletionsPHONE SCREEN · Seen Nov 2026
- Corporate Card Expense RulesOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- In-Memory Priority Task SchedulerPHONE SCREEN · Seen Oct 2026
- Limit an Organization Tree's HeightPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Median Across N Sorted ArraysPHONE SCREEN · Seen Oct 2026
Similar questions at other companies
- Path Through an O/X Grid Using Only Right and DownAmazon · PHONE SCREEN
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Find Robots by Blocker DistancesUber · PHONE SCREENUber interview questions
- Factory Cost — Minimum Cost Across StagesStripe · PHONE SCREENStripe interview questions
- Maximum Rocks on a North-or-East Grid PathGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Maximal SquareAirwallex · PHONE SCREENAirwallex interview questions
- Process Minesweeper Game OperationsAmperity · PHONE SCREENAmperity interview questions
- Count Islands in a Binary GridApple · PHONE SCREENApple interview questions
- Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Pacific Atlantic Water FlowAnduril · PHONE SCREENAnduril interview questions
Interview reports that mention this question
Rippling interview experiences
- Rippling No-Level Older Reports: 31 Experiences (Part 6 of 6)
- Rippling No-Level Onsite Loops: 24 Experiences (Part 5 of 6)
- Rippling No-Level Screens and Loops: 18 Experiences (Part 4 of 6)
- Rippling No-Level Coding Screens: 37 Experiences (Part 3 of 6)
- Rippling No-Level Design, Screens: 30 Experiences (Part 2 of 6)