Google Interview Question for Software Engineer / Developers






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

Sort the array.
Consider a line-segment starting from (A[0],0) and ending at (A[n-1],0). Divide this line-segment into k-2 equal pieces. Find out the closest values to those pieces in the array A - these k values are the answer.

- Gopal June 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not DP. Actually. The answer for 3 and 4 is different.
The solution for 3 is not necessary contained in the solution for 4.

greedy is the right track. O(nlgn) solution exist.

- T January 22, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

take diff from k for each number O(n) and then sort the numbers O(n log n)..then we can get the soln in O(k).

k < n < n log n...therefore overall order O(n log n)

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

What does "take diff from k" mean? Let the N numbers be a1, a2...aN. Are you talking about computing a1-k, a2-k ... aN-k? But I think the problem is about selecting k numbers among a1..aN. I cannot see how this can be solved in O(nlogn).

- ranger March 30, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sort first and then take diff

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

I do not quite understand this problem. Can anybody elaborate a little bit (maybe through an example?) Thanks!

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

i think we can solve the problem by simply sorting it

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

>>> minimum difference of all the possible pairs of k numbers is maximum
wht does this mean? is it like sum of differences of all possible pairs of k numbers? Then sort the numbers and pick 1 minimum and (k-1) maximum numbers....

- pm April 06, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

will sorting the numbers first and choosing the numbers from both ends in pairs give the maximum difference?

For example, suppose we have 5 numbers, 1, 8, 12, 16, 20 (sorted), when k=2, we can choose: 1 and 20, because they give the maximum distance. When k=3, we can choose any numbers in 8, 12, and 16, because 1, 20, and this number give the same total difference. When k=4, if we want to get the maximum difference, the 3rd number has to be 8, so the 4th can be chosen as 16, which has the greatest difference from 8, but not 12.

You got the idea. If we were to choose k numbers, if:
1) k is odd, we choose (k-1)/2 from both ends, and then choose the median.
2) k is even, we choose k/2 from both ends.

This sounds naive. But it seems to work. Complexity would be O(nlgn) for initial sorting.

- peng April 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

{1,2,3,9,20} k=3 --> result { 1,9,20}
Do you still think your solution is correct?

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

a better extreme example can be:

{1,2,3,19,20} k=3 --> result { 1,19,20}. The min difference for any pairs of these 3 elements is 1, however we can easily find another 3 elements {1,3,20}, the min difference is: 2

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

Sort the sequence {a1,a2,....an-1,an}
Obviously k<=n
keep pointers at a1 and an
now .. if an - a2 > an-1 - a1 choose a2 else choose a1 (accordingly move the pointer)
keep choosing until u have k numbers

- Bala May 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

consider the nos 1,4,6,19,20 aftr sorting
if k=3 then acc to your algo u'll select 1,19,20

the quest says "the minimum difference of all the possible pairs of k numbers" so the min diff is 1 for 19 & 20

Acc to me you should select 1,6,20... 6 coz center of remaining and one can build on logic from here onwards

- kc May 24, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Refer problem Aggressive cows problem on spoj. Its the same problem as this one.
First we sort the array . Then we do a binary search on the answer in the range [0 maxVal-minValue+2].

- Anonymous March 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Refer problem Aggressive cows problem on spoj. Its the same problem as this one.
First we sort the array . Then we do a binary search on the answer in the range [0 maxVal-minValue+2].

- madhav March 16, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1. First sort the array A
2. Then solve this problem using DP as follows:
Let f(i, j) denote, maximum difference if you select i numbers from the array, starting from index j and element at index j must be selected.
f(i, j) = max { min( f(i-1, m), A[m]-A[j] ) }, m=j+1 to n-1, if i<=n-j
f(i, j) = -Infinity, if i >n-j
the goal is to solve f(1, k), (note: it is always safe to include 1st element in the k numbers), complexity is O(n^2)

- jiex.cs January 23, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

DP able: O(K*n^2)
f(n,k) = Max( MIN(f(m, k-1), A[n-1]- A[m-1]), .... ) | m=1...n-1

- BIN December 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Solved here:
geeksforgeeks.org/k-numbers-difference-maximum-minimum-k-number-minimized/

- Rajat.nitp July 05, 2020 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

another DP problem?
I wish I had so many DP problems in my interview... sigh

- geniusxsy January 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think an nlogn greedy alg. is good enough

- fox January 22, 2010 | Flag
Comment hidden because of low score. Click to expand.
-1
of 3 vote

binary search :)

- MD February 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

are you smiling at how dumb you are?

- Mahesh April 13, 2010 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

he is not dumb. binary search can solve the problem

- Anonymous December 29, 2012 | 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