Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Matrix Search Without Full Scan

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Matrix Search Without Full Scan. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Hiver organizes illustrative thread-priority values in a matrix where every row and every column is sorted in ascending order. Given this matrix and a target priority, return the coordinates of any cell containing the target without scanning every cell.

Use an algorithm that exploits both sorting dimensions. Return [-1, -1] when the target is absent.

Formal Specification

Implement search_priority_matrix(matrix, target), where matrix is a non-empty rectangular list of lists of integers and target is an integer. Return a two-element list [row_index, column_index]. If duplicate target values exist, any matching coordinate is valid.

Constraints

  • 1 <= rows, columns <= 10^4
  • rows * columns <= 10^6
  • -10^9 <= matrix[row][column], target <= 10^9
  • Each row and column is sorted in non-decreasing order
  • The matrix is rectangular

Function Signature

def search_priority_matrix(matrix, target):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output