Interview Question for Software Engineer / Developers






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

Sort the array in O(nlogn), then go over sorted array and find the necessary numbers in O(n). Overall O(nlogn) + O(n) ~ O(nlogn).

- fiddler.g July 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can this be done without sorting the array?

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

int FindLeastDiff(int a[], int N, int & i, int & j)
{
   i = j = -1;
   int ii = 0, least_diff = -1;

   for (int jj = 1; jj < N; ++jj)
   {
      int diff = abs(a[ii] - a[jj]);

      if (diff < least_diff || least_diff < 0)
      {
         least_diff = diff;
         i = ii;
         j = jj;
      } else
         ii = jj;
   }

   return least_diff;
}

- fiddler.g July 11, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is far from correct.
Try it on 4 numbers 1 8 7 6.

- ftfish July 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think this is wrong :)

- fiddler.g July 11, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes.
There're some O(nlogn) algorithms which can solve the 2D closest points problem. Reducing the problem to 1D gives the answer.

Note that if this problem can be solved under O(nlogn) time, the classical element uniqueness problem can also be solved under O(nlogn) time, which contradicts the known lower bound of that problem.

But, when randomized algorithms and something like hashtable are allowed, it is also possible to solve this in O(n) expected time. google helps if you want to know more.

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

Also we can use modified version of quicksort's partition operation.
Select the pivot element, then during the partitioning we can get the following:
[ left side of array < pivot ] [ pivot ] [ right side of array > pivot ]
minL = min(left side of array)
minR = min(right side of array)

So the possible elements should be (minL, pivot) or (pivot, minR).
Then recursively perform this for left and right subarrays and get the necessary result.

Time complexity O(nlong).

- fiddler.g July 11, 2010 | 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