Data Structures & AlgorithmsModerateProduct-company MCQ pattern
What is the length of the shortest contiguous subarray of [1, 4, 4, 2, 3, 1, 5] whose sum is at least 9?
- A. 4
- B. 2
- C. 1
- D. 3
Correct answer: D. 3
No two adjacent elements reach 9 (the largest pair sum is 4 + 4 = 8, then 6 for 1 + 5), while 1 + 4 + 4 = 9 and 3 + 1 + 5 = 9 give length 3. Picking 4 and 5 for length 2 ignores contiguity.
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 contiguous subarrays of [1, 2, 3, −3, 3, 1] have sum exactly 3? (A prefix-sum + hash-map method counts them in O(n).)
- 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 =…