Data Structures & AlgorithmsHardGATE CS · DSA
The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [1, 3, 3, 1, 1, 3, 3, 3]. What candidate and final counter does it return, and is that candidate really a majority (> n/2)?
- A. candidate 3, counter 2; yes
- B. candidate 3, counter 2; no
- C. candidate 1, counter 2; yes
- D. candidate 3, counter 3; yes
Correct answer: A. candidate 3, counter 2; yes
Tracing the vote (increment the counter on a match with the candidate, decrement on a mismatch, replace the candidate when the counter hits 0) leaves candidate 3 with counter 2. Checking by counting shows 3 occurs 5 times out of 8, so it is a true majority — the algorithm must always be verified with a second pass.
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
- Array [90, 6, 26, 27, 39, 47, 79] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary search (decide…
- Two sorted linked lists [12 → 17 → 24 → 26 → 32] and [8 → 10 → 12 → 38] are merged with the standard O(n+m) two-pointer merge (always take…
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [1, 1, 2, 2, 1, 1, 1, 1, 2, 0] (values 0, 1, 2…
- Heights [8, 6, 3, 2, 6, 8, 8, 4, 7, 1] represent vertical lines at each index. What is the maximum water area (min(height) × horizontal…
- Kadane's algorithm runs on the array [5, 4, 4, -3, -5, -1, 8] to find the maximum sum of a contiguous subarray. What sum does it return?
- What is the sum of the minimum element over every contiguous subarray of [9, 4, 5, 3, 1, 9, 8, 1]?