Question 41
MediumWhich algorithm finds the majority element (appearing more than n/2 times) in O(n) time and O(1) space?
- A.
Popular companies
Prepare company by company
Hiring pattern, syllabus, past questions and mocks for each company.
All companiesPractice by subject
Data Structures and Algorithms
Which algorithm finds the majority element (appearing more than n/2 times) in O(n) time and O(1) space?
Binary search
Boyer–Moore voting
Merge sort
Kadane's algorithm
Sign up free to see the answer and a step-by-step explanation.
What does the Dutch national flag algorithm do?
Sorts an array of 0s, 1s and 2s in one pass with three pointers
Rotates an array
Merges two sorted arrays
Finds the maximum subarray
Sign up free to see the answer and a step-by-step explanation.
An array of size n contains numbers 1 to n with one missing and one repeated. Which method finds both in O(n) time and O(1) extra space?
Binary search
Sorting the array
Using the sum and sum of squares (or XOR) of the elements compared with 1..n
Using a nested loop
Sign up free to see the answer and a step-by-step explanation.
How can you rotate an array of n elements left by k positions in O(n) time and O(1) space?
Reverse the first k, reverse the rest, then reverse the whole array
Sort the array
Shift one position k times
Copy into a new array
Sign up free to see the answer and a step-by-step explanation.
With a difference array, how fast can you add a value to every element in a range [l, r]?
O(log n) per update
O(n) per update
O(1) per update, with an O(n) pass at the end to get the final values
O(n²)
Sign up free to see the answer and a step-by-step explanation.
Where is element a[2][3] of an int a[4][5] array (4-byte ints, row-major) relative to the base address?
56 bytes
60 bytes
52 bytes
44 bytes
Sign up free to see the answer and a step-by-step explanation.
In column-major order, what is the element offset of a[i][j] in an R × C array?
i × C + j
j × R + i
i × R + j
j × C + i
Sign up free to see the answer and a step-by-step explanation.
What is the time complexity of merging two sorted arrays of sizes m and n?
O(m × n)
O(log(m + n))
O((m + n) log(m + n))
O(m + n)
Sign up free to see the answer and a step-by-step explanation.
A sorted array of n elements must stay sorted after inserting a new value. What is the time complexity, even though the position is found by binary search?
O(1)
O(n)
O(log n)
O(n log n)
Sign up free to see the answer and a step-by-step explanation.
Which problem is solved by keeping a running prefix sum and a hash map of earlier prefix sums?
Rotating the array
Finding the median
Sorting the array
Counting subarrays whose sum equals k
Sign up free to see the answer and a step-by-step explanation.