Data Structures & AlgorithmsVery hardTCS NQT · Infosys pattern
The Boyer–Moore majority vote algorithm (candidate/counter, no verification pass) runs on [7, 5, 2, 5, 5, 5, 2, 7]. What candidate and final counter does it return, and is that candidate really a majority (> n/2)?
- A. candidate 5, counter 0; yes
- B. candidate 5, counter 1; no
- C. candidate 5, counter 0; no
- D. candidate 7, counter 0; no
Correct answer: C. candidate 5, counter 0; no
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 5 with counter 0. Checking by counting shows 5 occurs 4 times out of 8, so it is not 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
- What does this loop-counting code print? (Python) python n, c = 45, 0 i = 1 while i < n: j = i while j < n: c += 1 j = 2 i += 1 print(c)
- Array [83, 87, 90, 43, 45, 48, 62, 75, 82] is a sorted array rotated at pivot 6. Using the standard O(log n) rotated-array binary search…
- Using a sliding window of fixed size with a character-frequency count, how many substrings of "AAABAAABCB" are anagrams of "BA"?
- Heights [6, 3, 1, 5, 9, 8, 9, 6, 4] represent vertical lines at each index. What is the maximum water area (min(height) × horizontal…
- Kadane's algorithm runs on the array [-9, 1, 7, 5, 7, -3, 0, -7, 1] to find the maximum sum of a contiguous subarray. What sum does it…
- QuickSort (last element as pivot, Lomuto-style partition) sorts [57, 6, 37, 10, 15, 58]. Counting one comparison per element checked…