Validate a Nonogram Grid
Updated
Problem statement
Given a rectangular binary matrix and a clue list for every row and column, return whether all clues describe the matrix exactly. 0 is black and 1 is white.
Read rows left to right and columns top to bottom. Each clue lists the lengths of the consecutive black-cell groups, in order. A group continues until a white cell or the end of the line. Separate runs require at least one white cell; leading and trailing white cells do not create runs. An empty clue list [] describes an entirely white line.
For example, the row clue [2,1] matches [1,0,0,1,0], [0,0,1,1,0] and [0,0,1,0,1]. It does not match [1,0,1,0,0] because the run order is reversed, or [1,0,0,0,1] because the black cells form one run of length three.
Validate the supplied clues as they are; do not change them to fit the grid. All row clues and all column clues must match.
Function
validateNonogram(matrix: int[][], rowClues: int[][], columnClues: int[][]) → booleanExamples
Example 1
matrix = [[1,1,1,1],[0,1,1,1],[0,1,0,0],[1,1,0,1],[0,0,1,1]]rowClues = [[],[1],[1,2],[1],[2]]columnClues = [[2,1],[1],[2],[1]]return = trueEvery ordered run list matches. The first row has no black cells and uses an empty list.
Example 2
matrix = [[1,1,1,1],[0,1,1,1],[0,1,0,0],[1,1,0,1],[0,0,1,1]]rowClues = [[],[],[1],[1],[1,1]]columnClues = [[2],[1],[2],[1]]return = falseThe second row contains one black run, but its supplied row clue is empty. Other mismatches also occur.
Example 3
matrix = [[1,1],[0,0],[0,0],[1,0]]rowClues = [[],[2],[2],[1]]columnClues = [[1,1],[3]]return = falseThe first column is [1,0,0,1], which has one run of length 2, not two separated runs of length 1. The original negative result is correct.
Constraints
- For this exercise, assume a rectangular matrix with
1 <= R,C <= 30and cells in{0,1}. rowClueshas exactlyRlists andcolumnClueshas exactlyClists.- For this exercise, assume each clue list has at most its line’s length entries, all positive integers no greater than that line’s length. Clues may be infeasible and should then return
false. Kdenotes the total number of entries in all supplied clue lists. Empty lists are allowed;[0]is outside the input contract.
More Karat interview questions
- Do Two Nodes Share a Proper Ancestor?PHONE SCREEN · Seen Sep 2021
- Parent and Child Counts for Every NodePHONE SCREEN · Seen Sep 2021
- Validate a Latin-Square MatrixPHONE SCREEN · Seen Aug 2020
- Nodes With Zero or One ParentPHONE SCREEN · Seen May 2020
- Lowest Common Ancestor in a Rooted TreeONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Matrix EscapeByteDance · PHONE SCREENByteDance interview questions
- Minimum Time to Spread Through a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Sudoku Board ValidationConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
- Reachable Exits in an Obstacle GridWhatNot · PHONE SCREENWhatNot interview questions
- Apply a 2048 Board MoveGlean · PHONE SCREENGlean interview questions
- Spiral Order with ObstaclesTemu · PHONE SCREENTemu interview questions
- Find Robots by Blocker DistancesUber · PHONE SCREENUber interview questions
- Incremental Hard Attention with a KV CacheOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- S-Shaped Matrix TraversalFox Corporation · PHONE SCREEN