Baidu Interview Question for Financial Software Developers






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

Is there any method to find max distance neighboring number where the array is integer array in O(n)?

- If_else July 04, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If its an integer array, there is a possibility to use hash table equal to size of integer bounds. I cant think of any other method for that.

- nitarsh July 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

how? hash table isn't an "ordered" data structure (like BST)? How could you find adjacent number (in sorted order) efficiently in an hash table; I mean in O(1) time?

- anon July 06, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

don't understand the question.
"max disntance two numbers" means the absolute value of the difference, such as |a-b|?
"two neighbouring numbers" means, e.g., (x1,x2), (x2,x3), (x3,x4) and so on?

- anoy July 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry, see an example
given x[]={2.0,1.0,9.0,-3.5}
then the answer is 7.0, because on the number axis, it is -3.5,1.0,2.0,9.0 from left to right.
distance between two neighbouring numbers are 1-(-3.5),2-1,9-2.
so the answer is 9-2=7
make sense?

- bonism July 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I doubt if there is better than O(n logn) solution. It is related to array uniqueness problem. Look in Wikipedia.

- anon July 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Call first element the pivot

Find largest number > pivot and to the right of array. Call it A. If you cannot find one A = pivot
Find smallest number < pivot and to the right of array. Call it B. If you cannot find one B = pivot

Ans : A-B

- Anonymous July 05, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

lol... u stupid fellow didn't get the problem right! the question is NOT asking to find max-min of an array (u need not to apply divide & conquer to get max/min, btw).

- anon July 05, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sort the array O(nlg(n)). Then in O(n) time i.e. single pass, you can get the maximum distance.

- NEO August 03, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Awesome solution !!!! Now every problem can be solved in O(n) or even better O(1)

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

the question explicitly specifies UNSORTED array right..? Sorting the array in O(nlgn) and then finding the max distance exceeds O(n)...

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

Heapify the array - takes O(n). Then since every node satisfies the heap property (for instance, so that every parent > children) find the max difference between each parent and its 2 children (which represent neighboring numbers) - again O(n). Overall: O(n).

- Anonymous April 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

We can use HashMap too..it will give the answer in O(n) time...

- joelnishan May 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

How will that be O(n) in the worst-case? hash table has O(n) worst-case complexity and O(1+a) average case (where a is the load balance factor).

- Anonymous May 11, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

there is an O(n) algorithm. use n bucket with the same size of (max-min)/(n-1) and hash each element into the corresponding bucket, then the maximum distance would >= (max-min)/(n-1), which would only appear in neighbor bucket (skipping empty buckets). then you know what to do.

- lambda2fei August 03, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

just use count sort

codeļ¼š

#include <cstdio>
#include <cstdlib>
#include <memory.h>
using namespace std;

int csort(int key[], int len)
{
	int *sort;
	int max = key[0];
	int min = key[0];
	int step = 0;
	int tmp = 0;
	for(int i = 0; i < len; i++)
	{
		if(key[i] >= max)
			max = key[i];
		if(key[i] <= min)
			min = key[i];
	}

	step = -min;

	for(int i = 0; i < len; i++)
	{
		key[i] = key[i] + step;
	}

	sort = (int *)malloc((max - min + 1)*sizeof(int));
	if(sort == NULL)
		printf("malloc error\n");
	memset((int *)sort, 0, (max - min + 1)*sizeof(int));

	for(int i = 0; i < len; i++)
	{
		sort[key[i]] = 1;
	}

	for(int i = 1; i<max - min + 1; i++)
	{
		sort[i] += sort[i-1];
	}
	
	for(int i = 0; i < max - min + 1; i++)
	{
		if(sort[i] == tmp)
			continue;
		else
		{
			key[sort[i]-1] = i;
			tmp = sort[i];
		}
	}

	for(int i = 0; i < len; i++)
	{
		key[i] = key[i] - step;
	}
        free(sort);
	sort = NULL;
	
	return 0;
}

int count_max(int key[], int len)
{
	int max = 0;
	for(int i = 1; i < len; i++)
	{
		if((key[i] - key[i-1]) >= max)
			max = key[i] - key[i-1];
	}
	return max;
}

int main()
{
	int key[10] = {3,7,9,8,-22,6,22,11};

	csort(key, 8);
	for(int i = 0; i < 8; i++)
		printf("%d ",key[i]);
	printf("\n");
	printf("The max distence is:%d\n",count_max(key,8));
	
}

- Tough.Cheung April 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

input is not integer.

- Passerby_A March 16, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void main(String[] args){
DistanceBetweenNo db = new DistanceBetweenNo();
double[] d_arr = {2.0,1.0,9.0,-3.5};
Arrays.sort(d_arr);
double max = d_arr[d_arr.length - 1] - d_arr[d_arr.length - 2];

System.out.println("Max difference: " + max);
}

- Anonymous March 25, 2017 | 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