FreeCareerPath
Build the PrimitiveMedium

Search a 2D Matrix

Write `search_matrix(matrix, target)` returning `True` if the value is present. Each row is sorted ascending, and the first value of a row is greater than the last value of the row above. search_matrix([[1,3,5,7],[10,11,16,20],[23,30,34,60]], 3) -> True search_matrix([[1,3,5,7],[10,11,16,20],[23,30,34,60]], 13) -> False Those two conditions together mean the matrix is one sorted sequence that happens to be stored in rows. Binary search over `[0, rows*cols)` and map each index back with `divmod(mid, cols)`. Searching for the row first and then within it also works and is a fine answer — say which you chose.

What to expect: A timer starts when you begin. Edit the starter code, run it against the test suite as many times as you like, then finish when you're done. The reference solution and interviewer follow-up questions unlock only after you finish.

Log in to start this session

Timed sessions and your results for "Search a 2D Matrix" are saved to your account — logging in takes a few seconds.

Search a 2D Matrix — FreeCareerPath