Data Structures & AlgorithmsHardProduct-company MCQ pattern
How many contiguous subarrays of [1, 2, 3, −3, 3, 1] have sum exactly 3? (A prefix-sum + hash-map method counts them in O(n).)
- A. 5
- B. 4
- C. 3
- D. 6
Correct answer: A. 5
Prefix sums are 0, 1, 3, 6, 3, 6, 7, and each earlier prefix that is 3 less than a later one marks a subarray: 0 → both 3s (2), the first 3 → both 6s (2), the second 3 → the last 6 (1), total 5. They are [0..1], [0..3], [2], [2..4], [4]; missing the ones that cross the −3 gives 3 or 4.
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
- 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…
- 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)?