Data Structures & AlgorithmsVery hardProduct company OA
The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [3, 4, 4, 4, 4, 1, 3, 4, 4]. What candidate and final counter does it return, and is that candidate really a majority (> n/2)?
- A. candidate 4, counter 4; yes
- B. candidate 3, counter 3; yes
- C. candidate 4, counter 3; yes
- D. candidate 4, counter 3; no
Correct answer: C. candidate 4, counter 3; 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 4 with counter 3. Checking by counting shows 4 occurs 6 times out of 9, 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
- A recursive function is defined as f(0) = f(1) = 1 and f(n) = f(n−1) + 2·f(n−2) for n ≥ 2. Using memoization, what is f(9)?
- The Dutch National Flag algorithm (single pass, three pointers low/mid/high) partitions [1, 0, 2, 2, 2, 1, 1, 0] (values 0, 1, 2 only) in…
- What is the postfix (Reverse Polish) form of the infix expression "e (a + b)- h / d", built with the standard stack-based conversion (^…
- A circular queue of capacity 5 starts empty and runs this sequence in order: enqueue(2), enqueue(3), enqueue(3), enqueue(2), enqueue(3)…
- A queue is implemented with two stacks (enqueue pushes to stack "in"; dequeue pops from stack "out", refilling "out" from "in" by…
- Keys 26, 63, 80, 39, 14, 99, 18, 93, 44 are inserted via linear probing (h(k) = k mod 11, probe by +1 on collision) into an initially…