Search a 2D Matrix II
Updated
Problem statement
You are given a grid of integers matrix and an integer target. In every row the values never decrease from left to right, and in every column they never decrease from top to bottom. Values may repeat.
Return true if target appears anywhere in matrix, and false otherwise.
Function
searchMatrix(matrix: int[][], target: int) → booleanExamples
Example 1
matrix = [[2,5,9],[4,8,12],[7,10,15]]target = 10return = trueStart at the top-right corner, 9. It is smaller than 10, and everything left of it in that row is smaller still, so drop the top row and move down to 12.
12 is bigger than 10, and everything below it in that column is bigger still, so drop the last column and move left to 8. 8 is smaller than 10, so move down to 10. That is the target, so the answer is true.
Example 2
matrix = [[2,5,9],[4,8,12],[7,10,15]]target = 6return = falseFrom the top-right corner the walk goes 9 (too big, move left), 5 (too small, move down), 8 (too big, move left), 4 (too small, move down), 7 (too big, move left). Moving left from 7 leaves the grid, so 6 is not in the matrix and the answer is false.
Example 3
matrix = [[-3]]target = -3return = trueThe matrix has one cell, and it holds the target -3.
Example 4
matrix = [[1,3,3,8],[2,3,6,9]]target = 7return = falseValues may repeat, like the three 3s here. The value 7 does not appear: the first row jumps from 3 to 8 and the second row from 6 to 9. The answer is false.
Constraints
1 <= matrix.length <= 1001 <= matrix[i].length <= 100. All rows have the same length.-100000 <= matrix[i][j] <= 100000-100000 <= target <= 100000- Each row is sorted in non-decreasing order from left to right, and each column is sorted in non-decreasing order from top to bottom.
More Bloomberg LP Phone Screen questions
- N-ary Index-Path Encoder and DecoderPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Trapping Rain WaterPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Word SearchOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Fewest Coins CombinationPHONE SCREEN · Seen Nov 2026
- Recommend Films from Viewers Who Watched Everything You DidPHONE SCREEN · Seen Nov 2026
- Track New Values in Sorted OrderPHONE SCREEN · Seen Nov 2026
- Design Browser HistoryPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Mutable Stock Price Top KPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Sparse Vector and Matrix OperationsPHONE SCREEN · Seen Oct 2026
- Add Two Numbers IIPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Find the Safest Path in a GridAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
- Search a Row-Major Sorted MatrixMotive · ONSITE INTERVIEWMotive interview questions
- Grid Path With the Most ClearanceDocuSign · OADocuSign interview questions
- Largest Square Sub-Grid Within a Sum LimitC3.ai · OAC3.ai interview questions
- Server SelectionAirbnb · OAAirbnb interview questions
- Koko Eating BananasThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Max Area of Island Without Changing the GridFlexport · PHONE SCREENFlexport interview questions
- Find First and Last Position in a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Sudoku Board ValidationConfluent · PHONE SCREEN · ONSITE INTERVIEWConfluent interview questions
Interview reports that mention this question
Bloomberg LP interview experiences
- Bloomberg AI-Assisted Coding Round, All Levels: 14 Experiences
- Bloomberg SWE Intern Interview Questions: 68 Questions
- Bloomberg Interview Questions, Level Not Reported: 65 Questions
- Bloomberg Experienced SWE Interviews: 167 Questions (Part 1 of 2)
- Bloomberg New Grad Coding Questions: 121 Questions (Part 2 of 2)