Find Earliest Column With One
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Quick Answer: This question evaluates algorithmic problem-solving and optimization skills for working with a row-wise sorted binary matrix accessed via an API, focusing on search efficiency and query-cost awareness.
Constraints
- 1 <= m, n <= 100
- matrix[i][j] is either 0 or 1
- Each row is sorted in nondecreasing order
Examples
Input: ([ [0,0,0,1], [0,0,1,1], [0,0,0,0] ],)
Expected Output: 2
Explanation: Column 2 contains a 1 in the second row, and no column to the left contains any 1.
Input: ([ [0,0], [0,0] ],)
Expected Output: -1
Explanation: The matrix contains only 0s, so there is no valid column.
Input: ([ [1,1,1], [1,1,1] ],)
Expected Output: 0
Explanation: The very first column already contains 1s, so the answer is 0.
Input: ([ [0,0,0,0,1] ],)
Expected Output: 4
Explanation: There is only one row, and its first 1 appears at column 4.
Input: ([ [1] ],)
Expected Output: 0
Explanation: In this single-cell matrix, the only cell is 1, so the earliest column is 0.
Hints
- Because each row is sorted, finding a `1` means everything to its right is also `1`, so those columns cannot improve the answer.
- Try starting from the top-right corner. From there, each step can eliminate either one row or one column.