Code Room
CodingMediumcod-g420
Subject MatrixLevel Mid–Senior~20 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

Given an m x n matrix in which each row is sorted in ascending order left-to-right and each column is sorted in ascending order top-to-bottom (but rows are NOT globally ordered relative to each other), count how many cells equal a given target. Aim for O(m + n) using the staircase walk; do not binary search every row. Dimensions up to 300 x 300.

Implement
count_in_sorted_matrix(matrix: list[list[int]], target: int) → int
Examples
in[[[1,4,7,11],[2,5,8,12],[3,6,9,16],[10,13,14,17]],5]out1
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.