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?
- A. O(n²) time, O(1) space
- B. O(n) time, O(n) space
- C. O(n) time, O(1) space
- 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.
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
- The numbers 1, 2, 3, 4, 5 are pushed onto a stack in that order, and pops may happen at any time in between. Which output sequence is…
- Keys 15, 11, 27, 8, 12, 22, 29, 1 are inserted into a hash table of size 7 with h(k) = k mod 7 and separate chaining. What is the length…
- Keys 22, 33 and 44 are inserted in that order into an empty table of size 11 with h(k) = k mod 11 and quadratic probing hᵢ(k) = (h(k) +…
- Two sorted lists of 5 and 7 elements are merged by the standard merge procedure. What is the maximum number of comparisons it can make?
- A fixed-size sliding window of length 3 moves over [2, 1, 5, 1, 3, 2, 9, 1]. What is the largest window sum?
- How many element shifts does insertion sort perform to sort [4, 3, 2, 10, 12, 1, 5, 6] in ascending order?