Interview Question






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

sort two arrays.

- Anonymous September 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is there any solution without sorting the array?

- DashDash September 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

look for max and min element in array and stop as soon as difference between current found elements is 2 or more. complexity O(n).

- Anonymous September 04, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Can you please elaborate taking a simple example?

- DashDash September 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

int main()
{
        int a[] = { 5, 6, 5, 4, 7 };
        int i, n = sizeof(a) / sizeof(int);
        int max, min;

        max = min = 0;
        for (i = 1; i < n && a[max] - a[min] < 2; ++i)
        {
                if (a[i] > a[max])
                        max = i;
                else if (a[i] < a[min])
                        min = i;
        }
        if (a[max] - a[min] >= 2)
                printf("%d %d\n", a[max], a[min]);

        return 0;
}

Output: 6 4

- Anonymous September 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

But difference is least of numbers 5,6 or 5,4, 6,7 and not 6,4?

- DashDash September 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Find out the smallest and 2nd smallest number from the array.
Difference between the 2 nums will be the least in array...

- Anonymous September 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How about this input
1,3,7,8

- DashDash September 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

1. Construct a BST for the array - O(nlogn)
2.Traverse the tree and find a node which has
minimum difference with either its left or
right child in whole of the tree.

- Anonymous September 05, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

find differnce between every pair possible and find minimum (without sorting array)

- anunymous September 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You are so brilliant..

- Mahesh September 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is sample programme to get the desired result.


package randomTest;

public class LeastDifferenceNumber {

private int[] numberArray = new int[50];

private void sortArray(){
if(numberArray !=null && numberArray.length>0){
for(int i=1;i<numberArray.length; i++){
int pos = i-1;
int temp = numberArray[i];
while(numberArray[pos]>temp){
numberArray[pos+1]= numberArray[pos];
pos--;
}
numberArray[pos+1]=temp;
}
}
}

private int getLeastDifferenceIndex(){
int leastDifferencePos = 1;
int minDiff = -1;
if(numberArray !=null && numberArray.length>0){
for(int i=1;i<numberArray.length; i++){
int diff=numberArray[i]-numberArray[i-1];
if(diff<minDiff){
minDiff = diff;
leastDifferencePos = i;
}
}
}
return leastDifferencePos;
}


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LeastDifferenceNumber leastDifferenceNumber = new LeastDifferenceNumber();
leastDifferenceNumber.numberArray = new int []{1, 18, 22, 28, 8, 58, 22, 92};
leastDifferenceNumber.sortArray();
int leastDifferencePos = leastDifferenceNumber.getLeastDifferenceIndex();
System.out.println("Least Difference position is ->" + leastDifferenceNumber.numberArray[leastDifferencePos-1] + " and " + leastDifferenceNumber.numberArray[leastDifferencePos]);

}

}


Algorithm:
1) Sort the array.
2) Find the position of the two consecutive digits in array whose difference is lowest.

- Mritunjay Kumar September 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
	int a[] = { 5, 6, 5, 4, 7 };
	int i, n = sizeof(a) / sizeof(int);
	int max, min;
	
	
	max = a[0];
	min = a[1]; 
	for (i = 2; i < n; ++i)
	{
		if(abs(max - min) > abs(max - a[i]))
		   min = a[i];
		if( abs(max - min) > abs(a[i] - min))
			max = a[i];
	}
	
	cout << "max = " << max << " min = " << min << endl; 

	
	return 0;
}

- Anonymous September 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Result : max = 5 min = 5
Complexity: O(n)

- Anonymous September 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Result : max = 5 min = 5
Complexity: O(n)

- Anonymous September 06, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

When int a[] = { 5, 7, 9, 11, 12 };

max = 5 min = 7

Wrong.

- lupuslupus September 07, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This problem is akin to bin packing. Its not as simple as finding the max and min of the given input list. Instead you pack every element into a bin and then find the pair with the least difference.

i) Find the bin size - (Max-min)/n. For the above given example, this would be (12-5)/5 = 1.39.
ii) Create n bins. Ex: bin1 - (min to min+bin_size), bin2 - (min+bin_size to min+bin_size*2)..so on. For the above given example, this would be bin1 (5-6.39), bin2(6.39-7.79)...bin5(10.6-12)
iii) Insert each element into its appropriate bin based on its range (lower_bound, upper bound]. Make an exception for the last bin.
iv) Exit the algorithm if any duplicates are found. Otherwise, find the difference between the max and min of each bin and between bins. Keep track and return the pair with the least difference.

Running time - O(n).

- Anonymous September 07, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Isn't this basically doing bucket sort and then finding the pair with minimum difference?

- Anonymous September 08, 2010 | Flag


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