Binary Search on matrix
Last updated: March 7, 2026
Quick Overview
Given a 2D matrix of integers where each row is sorted in ascending order and the first integer of each row is greater than the last integer of the previous row, implement a function to perform a binary search on the matrix to determine if a target integer exists within it. The function should return true if the target is found and false otherwise.
Datadog
March 7, 2026228
12
4,570 solved
Given a 2D matrix of integers where each row is sorted in ascending order and the first integer of each row is greater than the last integer of the previous row, implement a function to perform a binary search on the matrix to determine if a target integer exists within it. The function should return true if the target is found and false otherwise.
This coding problem is frequently asked during Phone Screen at Datadog. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. Datadog expects candidates to write production-quality code, not just solve the puzzle.
What the Interviewer Expects
- Identify the correct data structure and algorithm for the problem
- Write clean, bug-free code with proper variable naming
- Analyze time and space complexity correctly
- Handle basic edge cases (empty input, single element)
- Communicate your thought process while coding
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- What is the worst-case input for your solution?
- What if the input doesn't fit in memory?
- How would your solution change if the input was sorted?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Problem Analysis
The problem requires us to determine if a target integer exists in a 2D matrix where each row is sorted in ascending order and the first integer of each row is greater than the last integer of the pre...
Approach
The algorithm will utilize binary search across the matrix. Here are the steps:
- Determine the number of rows (
m) and columns (n) in the matrix. - Initialize two pointers,
left(0) and `right...