Data Structures & AlgorithmsHardProduct-company MCQ pattern
How many UNIQUE triplets (as sets of values, ignoring order) of [−1, 0, 1, 2, −1, −4, −2, 3] sum to 0? (Sort + two pointers, skipping duplicates.)
- A. 6
- B. 5
- C. 4
- D. 7
Correct answer: B. 5
The unique triplets are (−4, 1, 3), (−2, −1, 3), (−2, 0, 2), (−1, −1, 2) and (−1, 0, 1), so 5. Without skipping the repeated −1, triplets such as (−1, 0, 1) and (−2, −1, 3) are found once for each −1 and the count comes out as 7.
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
- What is the time complexity of this Java method? java String build(int n) { String s = ""; for (int i = 0; i < n; i++) s = s + "a"; return…
- In Python, a list q is used as a queue: items are added with q.append(x) and removed with q.pop(0). If n items pass through the queue…
- What does this function print for the list 1 → 2 → 3 → 4 → 5 → 6? python def f(node): if node is None: return if node.next…
- slow and fast both start at the head of 1 → 2 → 3 → 4 → 5 → 6. Loop A: while (fast && fast->next) { slow = slow->next; fast =…
- What is the maximum number of comparisons top-down merge sort can make on 8 elements (any input order)?
- What is the minimum number of comparisons needed, in the worst case, to find BOTH the maximum and the minimum of 100 distinct numbers?