Interview Question for Software Engineer / Developers






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

Here is a O(n) solution.

curMin=MAX;
curMaxDiff=0;
for ele in array[0,n-1]
   if(ele < curMin)
       curMin=ele
   else if( (ele-curMin)>curMaxDiff )
       curMaxDiff=ele-curMin
end for
output curMaxDiff

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

Thanks!

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

Nice sol

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

Or take a better example, [ 7 9 5 6 3 2 ] Ans-> 2 [9 - 7]

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

According to the problem statement its the difference between any two numbers with the smaller number appearing after the larger number.

Thus for your example the answer must be [9-2] 7

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

Sorry abt the prev post :( .. my bad!

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

There is a easy nsquare soln. Are you looking for to reduce the complexity?

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

Hi,
also we can solve by taking the smallest and biggest element in the array, and the max difference would be answer
O(n) solution

- jack.watermelon April 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This can't be the solution. you need to take care of the index part as well(part of the question).

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

public void MaximumDifferenceBetweenTwoElements()
            {
                //The function assumes that there are at least two
                //elements in array.
                //The function returns a negative value if the array is
                //sorted in decreasing order.
                //Returns 0 if elements are equal


                //We take difference between the picked element and the minimum element found so far.
                //So we need to keep track of 2 things:
                //1) Maximum difference found so far (max_diff).
                //2) Minimum number visited so far (min_element).
                int [] arr = {2, 3, 10, 6, 4, 8, 1};
                
                int maxDiff = arr[1] - arr[0];
                int minElement = arr[0];
                for (int i = 1; i < arr.Length; i++)
                {
                    if (arr[i] - minElement > maxDiff)
                        maxDiff = arr[i] - minElement;
                    
                    if (arr[i] < minElement)
                        minElement = arr[i];
                }

                Console.WriteLine(maxDiff);
            }

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

Find out the max element in the array along with its index --> O(N)
Now find out the min element between 0 and index(got in the above step) --> O(index of Max num)

The answer is just the difference of Max and Min
Time: O(N)
Space: O(1)

- Rajnikanth February 26, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@ Rajinikanth..your solution fails...consider 7,9,5,1,6,3,2

- Sai June 05, 2013 | 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