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?
- A. O(log² n)
- B. O(n)
- C. O(log n)
- 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).
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Swap every two adjacent nodes of a linked list by relinking (not by swapping values). What is printed? python def swappairs(h): d = p =…
- Partition a linked list around x: all nodes with value < x come first, then the rest, each part keeping its original relative order. What…
- Two ways of finding the middle of a linked list with slow/fast pointers. What is printed? python h = build([25, 47, 35, 52, 83, 28, 17…
- Evaluate the postfix expression 3 5 4 6 1 ÷ − + + (all divisions are exact). What is its value, and what is the maximum number of operands…
- Count palindromic substrings by expanding around all 2n − 1 centres, and also count DISTINCT palindromic substrings. What is printed?…
- Top-down merge sort (split at mid = n // 2, recurse, then merge, stopping comparisons when one side runs out) sorts [33, 53, 13, 42, 11…