Data Structures & AlgorithmsModerateProduct-company MCQ pattern
Kadane's algorithm is run on [3, −4, 5, −1, 2, −6, 4]. What maximum contiguous subarray sum does it return?
- A. 14
- B. 5
- C. 6
- D. 3
Correct answer: C. 6
The best subarray is [5, −1, 2] with sum 6. Running sums restart after 3 − 4 = −1 < 0; then 5, 4, 6 (best), 0, 4. Adding all the positives (14) ignores contiguity, 5 is just the largest element and 3 is the whole-array sum.
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 auxiliary space used by the standard top-down merge sort on an array of n elements?
- In a singly linked list you are given only a pointer to a node X (not the last node) and no head pointer. How can X be deleted, and in…
- Which of these is NOT a typical application of a stack?
- 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…
- 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…