Data Structures & AlgorithmsVery hardTCS Digital · Infosys SP
The Boyer–Moore majority vote (candidate/counter, no verification pass) runs on [3, 5, 5, 7, 7, 7, 7, 7, 3]. What candidate and final counter does it return, and is that candidate really a majority (> n/2)?
- A. candidate 7, counter 3; no
- B. candidate 7, counter 3; yes
- C. candidate 7, counter 4; yes
- D. candidate 7, counter 5; yes
Correct answer: B. candidate 7, counter 3; yes
Tracing the vote leaves candidate 7 with counter 3. The candidate is only guaranteed correct when a majority exists; counting shows 7 occurs 5 times out of 9, so it is a majority.
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
- KMP failure function (lps[i] = length of the longest proper prefix of p[0..i] that is also a suffix). What is printed? python def lps(p)…
- The array [1, 2, 3, 4, 5, 6, 7, 8] is rotated RIGHT by 3 positions and then LEFT by 2 positions. What is the final array?
- Kadane's algorithm runs on [4, 0, 8, −3, −2, −2, −3, 2, −6]. What is the maximum sum of a non-empty contiguous subarray?
- How many characters must be deleted in total from two strings so that they become anagrams of each other? What is printed? python from…
- What does this loop-counting code print? (Python) python n, c = 33, 0 i = 1 while i < n: j = i while j < n: c += 1 j = 2 i += 1 print(c)
- Evaluate an infix expression of single digits with two stacks (operands and operators, usual precedence, left-associative). What is…