Data Structures & AlgorithmsHardIBM · Deloitte pattern
Kadane's algorithm runs on the array [-4, 3, 9, 3, 3, -6, 7, 4, 6] to find the maximum sum of a contiguous subarray. What sum does it return?
- A. 33
- B. 9
- C. 25
- D. 29
Correct answer: D. 29
Tracing Kadane's algorithm (reset the running sum to the current element whenever it drops to zero or below, else extend it) gives a best contiguous sum of 29, from index 1 to 8. 25 is the sum of the whole array (wrong when some prefix should be dropped), and 9 is just the single largest element, which ignores combining it with neighbours.
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
- Binary search for first/last occurrence runs on the sorted array [2, 5, 6, 16, 19, 19, 20, 21] for target 19. What (first index, last…
- Array [1, 2, 3, 4, 5, 6, 7, 8, 9] is left-rotated by d = 3 positions using the reversal algorithm (reverse first d, reverse rest, reverse…
- Two sorted linked lists [8 → 33 → 35 → 40] and [2 → 13 → 16] are merged with the standard O(n+m) two-pointer merge (always take the…
- A circular queue of capacity 4 starts empty and runs this sequence in order: enqueue(2), enqueue(6), dequeue(), dequeue(), enqueue(1)…
- Keys 13, 28, 91, 42, 33 are inserted via linear probing (h(k) = k mod 7, probe by +1 on collision) into an initially empty table of size 7…
- Using a sliding window (shrink from the left whenever more than k distinct characters are in the window), what is the length of the…