Data Structures & AlgorithmsModerateProduct-company MCQ pattern

A sorted array of n integers is given. Using the two-pointer technique, what are the time and extra space needed to decide whether some pair sums to a target T?

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

Correct answer: C. O(n) time, O(1) space

Start with pointers at both ends; if the sum is too small move the left pointer right, if too large move the right pointer left. Each step discards one element, giving O(n) time and two variables of space; the hash-set method is also O(n) time but needs O(n) space.

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

Exams that ask this topic