Binary Search a Quota Table Whose Rows Continue One Another

Last updated: November 29, 2026

Quick Overview

Given an m x n table where each row is sorted ascending and the first value of each row is greater than the last value of the previous row, return whether a target value exists, in O(log(mn)) time.

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

0

0

0 solved


Given an m x n table where each row is sorted ascending and the first value of each row is greater than the last value of the previous row, return whether a target value exists, in O(log(mn)) time.

An Easy Google screen problem that checks whether you can treat a 2D layout as a virtual 1D sorted array. Interviewers look for one binary search with index mapping, not two searches with fiddly boundaries.

What the Interviewer Expects
  • Recognizes the table is globally sorted in row-major order
  • Maps a mid index to (mid // n, mid % n)
  • Writes a bug-free binary search with correct bounds
  • Achieves O(log(mn)) time and O(1) space
  • Handles empty tables
Key Topics to Cover
Binary Search
Index Mapping
Sorted Matrix
Virtual Arrays
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
  • Return the position of the first value greater than or equal to the target.
  • Compare with doing a binary search on the first column, then within a row.
  • What if rows are stored on separate machines?
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
  • Input: table (row-major globally sorted) and target.
  • Output: True if present.
  • Must be logarithmic in the number of cells....
High-Level Design

Because every row continues the previous one, reading the table row by row yields one sorted sequence of length m*n. Run a standard binary search over indices 0..m*n-1, converting each probe with ...


Submit Your Answer
Markdown supported