Adobe Interview Question
SDE1sCountry: India
Here's working code of the algorithm:
#include <iostream>
#include <string>
// I tried to take into account all edge cases but I may have missed one.
std::string findMajoritySorted(int a[], int beg, int end) {
if (beg < 0 || beg >= end) return "Invalid Input";
int mid = beg + (end - beg) / 2;
int candidate = a[mid];
int startMid = mid;
int end1 = end, beg1 = startMid;
if (a[beg] == candidate && a[end - 1] == candidate) return std::to_string(candidate);
if (a[mid - 1] != candidate && a[mid + 1] != candidate) return "No Majority by test.";
int j = 0, k = 0;
while (end1 >= beg1) {
int mid = beg1 + (end1 - beg1) / 2;
if (a[mid + 1] > candidate && a[mid] == candidate) {
k = mid;
break;
} else if (a[mid] > candidate) {
end1 = mid - 1;
} else {
beg1 = mid + 1;
}
}
int end2 = startMid, beg2 = beg;
while (end2 >= beg2) {
int mid = beg2 + (end2 - beg2) / 2;
if (a[mid - 1] < candidate && a[mid] == candidate) {
j = mid;
break;
} else if (a[mid] < candidate) {
beg2 = mid + 1;
} else {
end2 = mid - 1;
}
}
return (k - j + 1 > ((beg + end) / 2) ? std::to_string(candidate) : "No Majority by binary search.");
}
int main() {
int sortedMajority[] = { 1, 1, 2, 2, 2, 2, 3 };
int sortedNotMajority[] = { 1, 1, 2, 2, 2, 3, 3 };
int NotMajority[] = { 1, 2, 3, 4 };
int singleElement[] = { 1 };
int fullDuplicatesElement[] = { 5,5,5,5 };
int twoElements[] = { 1, 2 };
std::cout << findMajoritySorted(sortedMajority, 0, 7) << std::endl;
std::cout << findMajoritySorted(sortedNotMajority, 0, 6) << std::endl;
std::cout << findMajoritySorted(NotMajority, 0, 4) << std::endl;
std::cout << findMajoritySorted(singleElement, 0, 1) << std::endl;
std::cout << findMajoritySorted(fullDuplicatesElement, 0, 1) << std::endl;
std::cout << findMajoritySorted(twoElements, 0, 2) << std::endl;
}
When the array is sorted then we can find a solution in O(logN) time. Here is the C++ solution:
#include <iostream>
using namespace std;
bool verify(int array[], int length, int start, int end, int candidate, int shift_factor) {
if(start<0 || start > length/2 || end >= length || end < (length-1)/2) {
return false;
}
if (array[start] == candidate && array[end] == candidate) {
return true;
}
if (array[start] != candidate && array[end] != candidate) {
return false;
}
int toShift = length/shift_factor;
if(toShift == 0) {
return false;
}
if(array[start] == candidate) {
return verify(array, length, start-toShift, end-toShift, candidate, shift_factor*2);
}
return verify(array, length, start+toShift, end+toShift, candidate, shift_factor*2);
}
int main()
{
int array [] = {1,1,2,2,2,2,3,3,5,5};
int length = 9;
int start = 0;
int end = length -1;
int mid = (start+end)/2;
int candidate = array[mid];
if (verify(array, length, start, mid, candidate, 2)) {
printf("Found: %d\n", candidate);
return 0;
}
printf("Not found\n");
return 0;
}
Your code failed on
{1,1,2,2,2,2,3} which has a majority element [2] but neither start or end is candidate.
and it's because this line:
if (array[start] != candidate && array[end] != candidate) {
return false;
}
There's no guarantee that the first or last, even less so both, elements must be the candidate.
It also fails on
{ 1, 2, 2, 2, 2, 3, 3 };
Even with the incorrect line of code removed. You need to check how many of the candidate elements there are, which can be done by binary searching both sides of the array in O(logN) still.
If the array isn't sorted, see my answer in your other post. Short answer: No.
- SycophantEve June 19, 2015If the array is sorted than the majority element is A[mid] if one exists. And I believe you'd be able to check if it is the majority element in O(logN) by binary searching to find the length of the run.
{1,1,1,2,3} 1 is the majority element A[mid] and you can binary search to find that there are 3 ones which is greater then n/2
{1, 1, 2, 3, 3} There is no majority element and you can find this by binary searching to find that there is one 2 which is less than n/2.
You could also speed this up by noting that if A[mid-1] && A[mid+1] != A[mid] then it cannot have a majority element. However, the converse isn't true. i.e. if A[mid-1] && a[mid+1] == a[mid] it still might not have a majority element.
For example:
{1,1, 2, 2, 2, 2, 3, 3} passes the test but doesn't have a majority element.
If anyone could correct me if I'm wrong I would be much obliged.