Data Structures & AlgorithmsModerateProduct-company MCQ pattern
For A = [3, 1, 4, 1, 5, 9, 2, 6] (0-based), the prefix array is P[0] = 0 and P[i] = A[0] + … + A[i−1]. Which expression gives the sum A[2] + A[3] + A[4] + A[5], and what is it?
- A. P[6] − P[1] = 20
- B. P[6] − P[2] = 19
- C. P[5] − P[2] = 10
- D. P[5] − P[1] = 11
Correct answer: B. P[6] − P[2] = 19
With this convention the sum of A[l..r] is P[r + 1] − P[l] = P[6] − P[2] = 23 − 4 = 19 (that is, 4 + 1 + 5 + 9). Using P[r] − P[l] drops the last element and gives 10.
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
- Linear search looks for a key that is NOT in an unsorted array of 100 elements. How many key comparisons does it make?
- What is the minimum number of stacks needed to implement a queue, if no other data structure may be used?
- What is the prefix (Polish) form of (A + B) (C − D)?
- Bubble sort is written with a flag that stops it when a full pass makes no swap. What is its time complexity on an array that is already…
- Kadane's algorithm is run on [3, −4, 5, −1, 2, −6, 4]. What maximum contiguous subarray sum does it return?
- What is the auxiliary space used by the standard top-down merge sort on an array of n elements?