Binary Search on matrix

Last updated: July 15, 2025

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 find a target value. The function should return the coordinates of the target as a tuple (row, column) if found, or (-1, -1) if the target is not present in the matrix.

DoorDash
Coding & Algorithms
Software Engineer
DoorDash
July 15, 2025
Software Engineer
Phone Screen
Coding & Algorithms
Medium

1

5

3,245 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 find a target value. The function should return the coordinates of the target as a tuple (row, column) if found, or (-1, -1) if the target is not present in the matrix.

This coding problem is frequently asked during Phone Screen at DoorDash. The interviewer is testing your ability to translate a problem into clean, working code while discussing time and space complexity. DoorDash expects candidates to write production-quality code, not just solve the puzzle.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Edge cases and input validation
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
Hash maps and frequency counting
Graph algorithms and traversal
Data structure selection and trade-offs
Tree structures and recursion
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
  • How would you parallelize this solution?
  • How would you modify your solution to handle streaming input?
  • What if the input doesn't fit in memory?
  • What is the worst-case input for your solution?
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 given problem requires us to search for a target value within a 2D matrix that has specific properties: each row is sorted in ascending order, and the first integer of each row is greater than the...

Approach
  1. Determine the dimensions: First, we need to get the number of rows and columns from the matrix.
  2. Binary Search Logic: We will use two pointers, left and right, initialized to 0 and `...

Submit Your Answer
Markdown supported

Related Questions