Data Structures & AlgorithmsModerateGATE CS · Product placement

For an n × n matrix whose rows and columns are both sorted ascending, what is the time of the staircase search for a key?

  1. A. O(log² n)
  2. B. O(n)
  3. C. O(log n)
  4. D. O(n log n)

Correct answer: B. O(n)

Starting at the top-right corner, every comparison discards a whole row or column, so at most 2n − 1 elements are examined. Binary searching each row would be O(n log n).

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic