Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA
Problem: Find the majority element (appearing more than half the time) of [2, 2, 1, 1, 1, 2, 2] using the Boyer–Moore voting algorithm, and report it.
- A. 3
- B. 2
- C. 1
- D. 4
Correct answer: B. 2
Boyer–Moore keeps a candidate and a counter that is cancelled by different elements; the surviving candidate is the majority if one exists. Choosing the first element without the counter gives the wrong answer here.
More Coding Interview Problems questions
- Problem: Kadane's algorithm finds the maximum sum of a non-empty contiguous subarray of [−2, 1, −3, 4, −1, 2, 1, −5, 4]. What is that…
- Problem: The array [1, 2, 3, 4, 5, 6, 7] is rotated right by 3 positions (the last 3 elements move to the front). What is the element at…
- Problem: Merging two sorted lists [1, 4, 7] and [2, 3, 8, 9] with the standard two-pointer merge, how many element comparisons are made in…
- Problem: In the expression print(232, (23)2) what are the two values printed, in order?
- Problem: What does this print? x = [1, 2, 3] y = x y += [4] z = x[:] z.append(5) print(len(x), len(z))
- Problem: What is printed by this snippet? d = {} d['b'] = 1 d['a'] = 2 d['b'] = 3 print(list(d.items()))