Leftmost Column With a One Through a Row-Sorted Matrix API

Last updated: November 29, 2026

Quick Overview

A binary matrix with each row sorted (all 0s before 1s) is only accessible through get(r, c) and dimensions(); return the index of the leftmost column containing any 1, or -1, using as few get calls as possible.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Medium

0

0

0 solved


A binary matrix with each row sorted (all 0s before 1s) is only accessible through get(r, c) and dimensions(); return the index of the leftmost column containing any 1, or -1, using as few get calls as possible.

A Google phone screen problem where the API constraint matters: the interviewer counts calls. They expect the staircase walk from the top-right with at most m + n calls, and a comparison with per-row binary search.

What the Interviewer Expects
  • Respects the black-box API and does not read the whole matrix
  • Starts at the top-right corner
  • Moves left on a 1 and down on a 0
  • Bounds calls by m + n
  • Compares with per-row binary search at O(m log n)
Key Topics to Cover
Staircase Search
Binary Search
API Constraints
Sorted Rows
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
  • Which approach is better when m is much smaller than n?
  • Return the row that has the most 1s.
  • Calls are expensive network RPCs; how would you batch them?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • BinaryMatrix exposes get(r, c) and dimensions() returning [m, n].
  • Each row is non-decreasing.
  • Output: smallest column index containing a 1 anywhere, else -1.
  • Minimize get calls (a ca...
High-Level Design

Stand at (0, n-1). If the cell is 1, this column has a 1, so record it and move left to search for an earlier one. If it is 0, every cell to its left in this row is also 0, so move down. The walk ne...


Submit Your Answer
Markdown supported