Center of the Largest Perfect X-Shape
Updated
Problem statement
Given a matrix matrix that contains only zeros and ones, return the center [row, col] of the largest perfect X-shape in it.
An X-shape has a center at some cell of matrix and 4 diagonal rays of equal length, going up-left, up-right, down-left and down-right from the center. The whole X-shape must fit inside the matrix. It is perfect when every one of its cells equals 1.
The size of an X-shape is the number of cells from the center to the end of one ray, counting the center. A single cell is an X-shape of size 1. A center with one cell on each ray is an X-shape of size 2.
If several perfect X-shapes have the largest size, return the center with the smallest row. If there is still a tie, return the one with the smallest column.
Rows and columns are numbered from 0.
Function
largestXShapeCenter(matrix: int[][]) → int[]Examples
Example 1
matrix = [[1,0,1,0,0,0,1],[0,1,0,1,0,1,0],[1,0,1,0,1,0,0],[0,1,0,1,0,1,0],[1,0,1,0,0,0,1]]return = [2,4]A size 3 X-shape needs two cells on each ray, so with 5 rows its center must be in row 2. At [2, 4], the rays pass through [1, 3], [1, 5], [3, 3] and [3, 5] and end at [0, 2], [0, 6], [4, 2] and [4, 6]. All of these cells are 1. The other row 2 centers fail: [2, 3] is 0, and from [2, 2] the up-right ray reaches [0, 4], which is 0. So [2, 4] is the only perfect X-shape of size 3.
Example 2
matrix = [[1,0,1],[0,1,0],[1,0,1]]return = [1,1]There is one perfect X-shape of size 2, with its center at [1, 1], and five perfect X-shapes of size 1, one at each 1. The largest is the one centered at [1, 1].
Example 3
matrix = [[1,1],[1,1]]return = [0,0]An X-shape of size 2 needs the four cells diagonally next to its center: up-left, up-right, down-left and down-right. So its center cannot be on an edge of the matrix. Every cell of a 2 x 2 matrix is on an edge, so there are only four perfect X-shapes, all of size 1. The tie goes to the smallest row, 0, and then the smallest column, 0.
Constraints
1 <= matrix.length <= 1001 <= matrix[i].length <= 100- All rows of
matrixhave the same length. matrix[i][j]is0or1.- There is at least one
1inmatrix.
More TikTok Online Assessment questions
- Count Subarrays Matching a Comparison PatternOA · Seen Nov 2026
- Count Suffix Word PairsOA · Seen Nov 2026
- Minimum Cost of Every K-Capable Model SetOA · Seen Nov 2026
- Count Subarrays with at Least K Equal-Fruit PairsOA · Seen Oct 2026
- Shell Command Counts with History ReferencesOA · Seen Oct 2026
- Product Minus Sum of DigitsOA · Seen Aug 2026
- Count 2x2 Submatrices by Black CellsOA · Seen Aug 2026
- Find All Local PeaksOA · Seen Aug 2026
- Count Case-Insensitive TripletsOA · Seen Aug 2026
- Optimize TikTok Reels ViewingOA · Seen Aug 2026
Similar questions at other companies
- Split a Matrix Into Four Partitions by Average (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Maximum Diamond SumHudson River Trading · OAHudson River Trading interview questions
- Robot Navigation Around LasersRoblox · OARoblox interview questions
- Apply a Reversi MoveAmazon · OA
- Flip, Invert, and Smooth a Binary ImageAbridge · OAAbridge interview questions
- Largest Square of OnesOpenAI · OAOpenAI interview questions
- Sparse Matrix MultiplicationNetflix · OANetflix interview questions
- First Completed Mingo LineEpic · OAEpic interview questions
- Shortest Path Around Rectangular ObstaclesDRW · OADRW interview questions
- Rotate a Square Matrix ClockwiseVisa · OAVisa interview questions
TikTok interview experiences
- TikTok AI Infra Intern LLM Round 2: 1 Experience
- TikTok AI Infra Intern DeepSpeed Round: 1 Experience
- TikTok AI Infra Intern Kernel Round: 1 Experience
- TikTok AI Infra Intern Interviews, 3 Rounds: 1 Experience
- TikTok AI Infra Intern Interview Timeline: 1 Experience
- TikTok AI Infra Intern Onsite Round 2: 1 Experience