Interview Question for SDE1s


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

Can be solved in linear time.

Partition the array into a negative side and a positive side. The switch point can be found in O(n) by iterating from right to left until a negative value is found (or until we fall off the left end). It could even be found in O(log(n)) with a modified binary search, but it's not worth the hassle, since the rest of the algorithm is O(n). Let idx_negative be the index of the rightmost negative integer, or -1 if there are no negatives.

Now, perform a conceptual merge sort where one array is the subarray A[idx_negative+1..n-1] and the other array is the array that you'd get if you travel right to left on the negative side, taking the absolute value of each element along the way.

In other words, the problem reduces to a merge step of mergesort if you look at your array as 2 independent sorted arrays (where the negative array is traversed right to left after getting the absolute value of each element). To avoid counting the same number twice, keep track of the previous number that would have been inserted in the output array of mergesort - if the current number is equal, don't count it, otherwise, count it.

Implementation in C:

#include <stdio.h>
#include <assert.h>

unsigned abs_distinct_merge(int arr[], size_t arr_sz, int idx_positive, int idx_negative, int last) {
	unsigned res = 0;
	while (idx_negative >= 0 && idx_positive < arr_sz) {
		int next;
		if (-arr[idx_negative] < arr[idx_positive]) {
			next = -arr[idx_negative];
			idx_negative--;
		} else {
			next = arr[idx_positive];
			idx_positive++;
		}
		if (next != last) {
			res++;
		}
		last = next;
	}

	while (idx_negative >= 0) {
		if (last != -arr[idx_negative]) {
			res++;
		}
		last = -arr[idx_negative];
		idx_negative--;
	}

	while (idx_positive < arr_sz) {
		if (last != arr[idx_positive]) {
			res++;
		}
		last = arr[idx_positive];
		idx_positive++;
	}

	return res;
}

unsigned abs_distinct(int arr[], int arr_sz) {
	int right_neg;
	for (right_neg = arr_sz-1; right_neg >= 0 && arr[right_neg] > 0; right_neg--)
		; /* Intentionally left blank */

	int idx_positive = right_neg+1;
	int idx_negative = right_neg;
	int last;

	if (idx_negative >= 0 && idx_positive < arr_sz) {
		if (-arr[idx_negative] < arr[idx_positive]) {
			last = -arr[idx_negative];
			idx_negative--;
		} else {
			last = arr[idx_positive];
			idx_positive++;
		}
	} else if (idx_negative >= 0) {
		last = -arr[idx_negative];
		idx_negative--;
	} else if (idx_positive < arr_sz) { 
		last = arr[idx_positive];
		idx_positive++;
	} else {
		assert(0);
	}

	return abs_distinct_merge(arr, arr_sz, idx_positive, idx_negative, last)+1;
}

static int array_buf[1024];
int main(void) {
	printf("Enter array size, followed by the elements. Array must be sorted.\n");
	printf("> ");

	size_t array_sz;
	while (scanf("%zu", &array_sz) == 1) {
		assert(array_sz <= sizeof(array_buf)/sizeof(array_buf[0]));
		size_t i;
		for (i = 0; i < array_sz; i++) {
			scanf("%d", &array_buf[i]);
		}
		unsigned distcount = abs_distinct(array_buf, array_sz);
		printf("Absolute distinct count = %u\n", distcount);
		printf("> ");
	}

	return 0;
}

- Anonymous June 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can be solved in linear time.

Partition the array into a negative side and a positive side. The switch point can be found in O(n) by iterating from right to left until a negative value is found (or until we fall off the left end). It could even be found in O(log(n)) with a modified binary search, but it's not worth the hassle, since the rest of the algorithm is O(n). Let idx_negative be the index of the rightmost negative integer, or -1 if there are no negatives.

Now, perform a conceptual merge sort where one array is the subarray A[idx_negative+1..n-1] and the other array is the array that you'd get if you travel right to left on the negative side, taking the absolute value of each element along the way.

In other words, the problem reduces to a merge step of mergesort if you look at your array as 2 independent sorted arrays (where the negative array is traversed right to left after getting the absolute value of each element). To avoid counting the same number twice, keep track of the previous number that would have been inserted in the output array of mergesort - if the current number is equal, don't count it, otherwise, count it.

Implementation in C:

#include <stdio.h>
#include <assert.h>

unsigned abs_distinct_merge(int arr[], size_t arr_sz, int idx_positive, int idx_negative, int last) {
	unsigned res = 0;
	while (idx_negative >= 0 && idx_positive < arr_sz) {
		int next;
		if (-arr[idx_negative] < arr[idx_positive]) {
			next = -arr[idx_negative];
			idx_negative--;
		} else {
			next = arr[idx_positive];
			idx_positive++;
		}
		if (next != last) {
			res++;
		}
		last = next;
	}

	while (idx_negative >= 0) {
		if (last != -arr[idx_negative]) {
			res++;
		}
		last = -arr[idx_negative];
		idx_negative--;
	}

	while (idx_positive < arr_sz) {
		if (last != arr[idx_positive]) {
			res++;
		}
		last = arr[idx_positive];
		idx_positive++;
	}

	return res;
}

unsigned abs_distinct(int arr[], int arr_sz) {
	int right_neg;
	for (right_neg = arr_sz-1; right_neg >= 0 && arr[right_neg] > 0; right_neg--)
		; /* Intentionally left blank */

	int idx_positive = right_neg+1;
	int idx_negative = right_neg;
	int last;

	if (idx_negative >= 0 && idx_positive < arr_sz) {
		if (-arr[idx_negative] < arr[idx_positive]) {
			last = -arr[idx_negative];
			idx_negative--;
		} else {
			last = arr[idx_positive];
			idx_positive++;
		}
	} else if (idx_negative >= 0) {
		last = -arr[idx_negative];
		idx_negative--;
	} else if (idx_positive < arr_sz) { 
		last = arr[idx_positive];
		idx_positive++;
	} else {
		assert(0);
	}

	return abs_distinct_merge(arr, arr_sz, idx_positive, idx_negative, last)+1;
}

static int array_buf[1024];
int main(void) {
	printf("Enter array size, followed by the elements. Array must be sorted.\n");
	printf("> ");

	size_t array_sz;
	while (scanf("%zu", &array_sz) == 1) {
		assert(array_sz <= sizeof(array_buf)/sizeof(array_buf[0]));
		size_t i;
		for (i = 0; i < array_sz; i++) {
			scanf("%d", &array_buf[i]);
		}
		unsigned distcount = abs_distinct(array_buf, array_sz);
		printf("Absolute distinct count = %u\n", distcount);
		printf("> ");
	}

	return 0;
}

- 010010.bin June 15, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Do you really need the search at the start? I believe this simple loop will work ( same idea though):

int absCount(std::vector<int> a) {
	int count = 0;
	for (int i = 0, j = a.size() - 1; i <= j;) {
		if (std::abs(a[i]) < std::abs(a[j])) {
			count++;
			j--;
		} else if (std::abs(a[i]) > std::abs(a[j])) {
			count++;
			i++;
		} else {
			count++;
			i++;
			j--;
		}
	}
	return count;
}

Correct me if I'm wrong. But since, as you said, the array is broken up into two subarrays already, we don't actually need to find the midpoint. The fact that it exists is enough. Still O(n) but should be about 1.5x as fast.

- SycophantEve June 18, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using Java we could use an HashSet. Just iterate once the array trying to insert the Abs() value of each array element in the hashset (if the value is already in the set it will not be inserted). At the end of the loop print hashset.size().

- Marco Scoppetta July 10, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More