Target Cells in One Block per Row and Column
Updated
Problem statement
Given an m x n integer matrix grid and an integer target, return true if, in every row and in every column, all the cells equal to target sit next to each other with no other value between them. Otherwise, return false.
- A row or column with a single
targetcell passes. - A row or column that has no
targetcell at all also passes. - Each row and each column is checked on its own. The blocks in different rows do not need to line up.
Function
isTargetContiguous(grid: int[][], target: int) → booleanExamples
Example 1
grid = [[1,2,2],[3,2,2],[3,3,1]]target = 2return = trueRow 0 has 2 in columns 1 and 2, and row 1 has the same. Columns 1 and 2 each have 2 in rows 0 and 1. Every block is unbroken, and the other lines have no 2.
Example 2
grid = [[2,1,2],[1,1,1]]target = 2return = falseRow 0 is [2,1,2]. Its two 2 cells have a 1 between them.
Example 3
grid = [[5,0],[0,0],[5,0]]target = 5return = falseEach row has at most one 5, so the rows pass. Column 0 is [5,0,5], and the 0 splits it.
Example 4
grid = [[1,2],[3,4]]target = 7return = trueNo cell holds 7, so no row or column breaks the rule.
Constraints
m == grid.lengthn == grid[i].length1 <= m, n <= 100-109 <= grid[i][j], target <= 109
More WhatNot Phone Screen questions
- Merge Sorted Squares of Two ArraysPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Adjacent Pair and Run RemovalPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- User Journey Paths from a Log StreamPHONE SCREEN · Seen Nov 2026
- Reachable Exits in an Obstacle GridPHONE SCREEN · Seen Oct 2026
- Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Attraction Route Without Reusing TrailsPHONE SCREEN · Seen Sep 2026
- Earliest Car for Each PassengerPHONE SCREEN · Seen Sep 2026
- Minimum Stores for a Shopping ListPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Validate a Chess Bishop, Rook, or Queen MoveGlean · PHONE SCREENGlean interview questions
- One-Nearest-Neighbor Classification with Manhattan DistanceOpenAI · PHONE SCREENOpenAI interview questions
- Unique Pairs in a 2D Matrix Summing to TargetAmazon · PHONE SCREEN
- Map Black Cells to Covering 2x2 BlocksSquare · PHONE SCREEN
- Trace an Eight-Direction Robot Until BlockedPinterest · PHONE SCREENPinterest interview questions
- Excel Training Data and Model Response AuditMicrosoft · PHONE SCREENMicrosoft interview questions
- Count Connect-Four Scoring LinesBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Completely Passable Grid LinesSplunk · PHONE SCREENSplunk interview questions
- Mark a Queen’s Attack BoardMotive · PHONE SCREENMotive interview questions
- Multiply Two MatricesAdobe · PHONE SCREENAdobe interview questions