Interview Question for Software Engineer / Developers






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

How about sorting the array and making every element equal to the mid point of the array? That way, it is a trade-off for elements on either side (greater and smaller) and no element has to be incremented a large number of times ?

Also, with the average, we face the risk of the average not being an integer, and thus the deferment may not be an integer either. In which case, we would never be able to achieve that value using the ++/-- operations allowed.

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

If the average is not an interger, doesn't that mean we would never be able to achieve the goal?

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

A[0] will be either -1 or 1
Check A[0] and put that value in rest of the array (the requirement is to make all elements of A equal).

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

I don't quite follow what you posted. There is no need to make any of A[i] to be 1 or -1. All elements can as well be made equal.

- hobocs December 08, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

If u take the average and then add / deferment all other elements by the difference from average , would that work?

- Guest12:54 December 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(N) solution:

1. In a single scan find the min and max elements of the array.
2. If their difference is more than 2 then its impossible .
3. If less than 2 then either all the elements are to be made equal to min/max/ min+1
4. In a single scan just increase/decrease each element accordingly

- Swapnil December 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(N) solution:

1. In a single scan find the min and max elements of the array.
2. If their difference is more than 2 then its impossible .
3. If less than 2 then either all the elements are to be made equal to min/max/ min+1
4. In a single scan just increase/decrease each element accordingly

- Swapnil December 08, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

consider the array of two elements A[0]=7 , A[1]=8
---------------
Consider any operation of unit one inc/decrement ... they can not be equal ???

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

I am sorry , i thought we can also keep an element unaltered. In which case my soln will work

- Swapnil December 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

1.take the avg of all no. if it is an integer then it possible else -1.

- Brutus January 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

And the ans will be Max(min-avg,max-avg)

- Brutus January 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Soln:
1. In a scan, find the max, min element and also see if all the elements are odd or even or there is a mixture
2. if the array contains a mixture of odd, even then return -1
3. Minimum number of operations is (Max - Min)/2.

here I am trying to bring each number close to the avg

- Himanshu Gupta June 25, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

How about bringing all the numbers to median

- 240V July 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

while solving this prob, I noticed that a solution is possible if and only if:

1. either all elements are odd
2. or all elements are even

otherwise no solution..

can some one validate this understanding?

- harleen June 28, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

thanks harleen... you helped me solve the problem


// if all are even elements or if all are odd elements, proceed, else return impossible
// avg = max + min / 2
// if element is less than avg, increment
// else, decrement
// do until max = min

oddCount = 0; evenCount = 0 , i=0;
while(i<A.length)
{
     if(A[i]%2==0)
          evenCount++;
     else
          oddCount++;
}
if(oddCount>0 && evenCount>0)
{
     return -1;
}

max = a[i];
min = a[i];
for(int i = 1; i<A.length; i++)
{
     if(a[i] > max)
          max = a[i];
     if(a[i] < min)
          min = a[i];
}
noOperations = 0;
while(max!=min)
{
     noOperations++;
     avg = (max + min) / 2;
     for(i=0;i<A.length;i++)
     {
          if(a[i]<avg)
               a[i]++;
          else
               a[i]--;
     }
     max--;
     if(avg > min)
          min++;
     else
          min--;
}

- kavin February 05, 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