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
Coding & Algorithms
Machine Learning Engineer
Datadog
March 7, 2026
Machine Learning Engineer
Phone Screen
Coding & Algorithms
Easy

228

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
Hash maps and frequency counting
Binary search and divide and conquer
Time and space complexity analysis
Sorting and searching
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample 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:

  1. Determine the number of rows (m) and columns (n) in the matrix.
  2. Initialize two pointers, left (0) and `right...

Submit Your Answer
Markdown supported

Related Questions