Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

Here is an iterative version of the code to find the duplicate element. It works in O(log n) time -

int nums[] = { 1,2,3,4,5,6,7,7,8};
	int low = 0, high = 9;
	while(low<high){
		if( nums[ (low + high)/2 ] <= (low+high)/2 )
		 	high = (low + high)/2 -1 ;
		else
			low = (low + high) /2 + 1;
	}
        printf("The duplicate number is  : %d ", nums[low] );

- codemaniac February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

As the array is sorted and as it srictly contains all numbers from 1 to n you can modify binary search to achieve O(logn) complexity. Because we can always expect the mid item in binary search. so if it is a hit, then the duplicate will be after this mid element, and if it is a miss the duplicate occurs before,

- Anonymous February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

This could be done in O(log n) time.
Sample code:
-------------------

int find_sorted_duplicate(int* arr, int start, int end)
{
	if (start > end)
		return -1;
	int mid = (start + end)/2;
	if (arr[mid] == mid)
	{
		if (arr[mid-1] == arr[mid])
			return arr[mid];
		return find_sorted_duplicate(arr, start, mid);
	}
	else
	{
		if (arr[mid+1] == arr[mid])
			return arr[mid];
		return find_sorted_duplicate(arr, mid, end);
	}
}
Test cases:
---------------
int main()
{
        // Test case 1
        int arr[] = { 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10};
	std::cout << find_sorted_duplicate(arr, 0, sizeof(arr)/sizeof(arr[0]) - 1) << std::endl;
        // Test case 2
        int arr1[] = { 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
	std::cout << find_sorted_duplicate(arr1, 0, sizeof(arr1)/sizeof(arr1[0]) - 1) << std::endl;
        // Test case 3
        int arr2[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10};
	std::cout << find_sorted_duplicate(arr2, 0, sizeof(arr2)/sizeof(arr2[0]) - 1) << std::endl;
        return 0;
}

- sand February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

let say { 1,2,3,3}

here take the sum of all elements :9

and do (n*(n-1))/2 :n is 4 here 4*3 /2 = 6

9-6 is 3..3 is duplicate

- shivacherukuri February 14, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(log n) complexity...

- bazinga February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if the given array is sorted O(log n) is possible, if it is not then O(n) by calculating the sum of the array and then subtracting from it, the sum of n numbers would give the answer

- hello world February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i am not getting the logic,bu substracting we can get the result.

- shailesh February 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about subtracting a[i] with a[i+1] as i moves from 0 to n.
The duplicate will be a[i] where a[i]-a[i+1] = zero.

- sandY February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will be a O(n) solution

- codemaniac February 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 4 vote

Sum of natural numbers is n(n+1)/2.... if sum of array exceeds this then that is your answer

- hector February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

if sum exceeds then what is the result. can u explain.

- Raising February 10, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Going by the question, i assume that the array is already sorted and the interviewer doesn’t bother about the running time of the program(it is not mentioned).

Running time: Worst-case is linear O(n).

private function findDuplicate(a:Array):void {
for ( var i:Number = 1 ; i< a.length; i++ ){
if ( a[i] == a[i-1] ){
trace(“Duplicate number is: “ + a[i]);
break;
}
}

- Snehasish Barman February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pseudo Code:

1: Get sum of all the numbers in the array.
2: Get the duplicate by calculating totalSum - (sizeOfArray*(sizeOfArray+1)/2)

- KT February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is just a twin of binary search: Find the place that a[i] == a[i-1]. O(logn) for sure.

- Ric February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int nums[] = { 1,2,3,4,5,6,7,7,8};
int res = nums[0];
for (int i = 1; i < nums[].length; ++i)
res = res ^ i ^ nums[i];
return res;

- Anonymous February 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Like a few others mentioned, it is as simple as summing up the numbers (from n+1) and then substracting the sum of n. The example in the question:

1 2 3 4 4 5 6 7 8 9 10 -> sum is 59
1..10 -> sum is 55

The difference is 4.

- Anonymous February 10, 2012 | 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