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
July 15, 20251
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
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
- 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 ProblemsSample 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
- Determine the dimensions: First, we need to get the number of rows and columns from the matrix.
- Binary Search Logic: We will use two pointers,
leftandright, initialized to 0 and `...