Morgan Stanley Interview Question for Software Engineer / Developers






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

say the nos are x to y with 1 missing no
temp1 = sum from 1 to y using the formula n(n+1)/2
temp2 = sum of 1 to x-1 using the formula n(n+1)/2
temp3 = sum of the given numbers
Missing no = temp1 - (temp3 + temp2)

- Avinash October 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

good expected answer

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

this solution is optimal for unsorted case.

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

what about overflow?

- Sophia March 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

<pre lang="" line="1" title="CodeMonkey4367" class="run-this">Java implementation of XOR algorithm, which is an O(n) solution, does not require sorted data input, and has no concern for overflow.

public static int findMissing(int[] arr) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;

// find the inclusive range of the set of sequential numbers
for (int i : arr) {
if (i < min) {
min = i;
}
if (i > max) {
max = i;
}
}

int missing = 0;
for (int i = 0; i < arr.length; ++i) {
missing ^= arr[i];
}
for (int i = min; i <= max; ++i) {
missing ^= i;
}

return missing;
}
</pre>

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

given set of sequential numerbs means its sorted

so assume u ll decrement all array values by (a[0]+1) then the data becomes 1,2,3,4,...,n
find the mising number here and add (a[0]-1) to it

- mail2vcp@gmail.com October 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The numbers in the array are sorted.
Get the median, compare (median-first) and
((index of median)-(index of first)),
(last-median) and ((index of last)-(index of median)).

Then you can determine which half this missing number is in.

Then take the sub array, get median, compare, half ...
Running time is O(lgn)

- sunbow.xs@hotmail.com October 06, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Best solution !!!

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

Good solution, provided the sequence is sorted.

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

I do not know why it is best solution.
mail2vcp is much better for sequential numbers.
The way approach is little bit creative but not very meaningful

- Sam October 27, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Great soln ..

- @ sunbow.xs August 31, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

say the set {1,2,...n}
define an array, arr[n], iter the set and put 1 in the corresponding element.
Missing numbers will have 0

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

Add them all up, get the sum of all expected numbers between x and y, the difference is the number missing.

- CKay February 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

modified binary search algorithm

- guest February 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="" line="1" title="CodeMonkey64489" class="run-this">Java implementation of XOR algorithm, which is an O(n) solution, does not require sorted data, and has no concern on overflow.

public static int findMissing(int[] arr) {

int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
int missing = 0;

/* xor all numbers in the set and find the inclusive
range of the set of sequential numbers */
for (int i : arr) {
missing ^= i;
if (i < min) min = i;
if (i > max) max = i;
}

// XOR all numbers within the range inclusively
for (int i = min; i <= max; ++i) {
missing ^= i;
}

return missing;
}
</pre>

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

Some of the solutions provided above expects difference of 1 within nos. in a sequence. Following solution can take any sequence provided the difference between nos. is consistent (in Arithmetic progression); except with the missing value.
//test cases:
int []arr = {-20,-14,-2,4}; //gives -8 , diff of 6
int []arr = {10,30,40}; //gives 20, diff of 10
int []arr = {2,6,8,10}; //gives 4, diff of 4

public static int myFindMissing(int []arr)
    {
        int diff=0;
        int prevDiff=Integer.MIN_VALUE;
        int i=1;
        for(;i<arr.length;i++)
        {
            diff = arr[i]-arr[i-1];
            if(diff!=prevDiff)
            {
                if(i==2)
                {
                    i=1;
                    prevDiff = diff;
                    break;
                }
                if(prevDiff==Integer.MIN_VALUE)
                    prevDiff=diff;
                else
                    break;
            }
        }       
        return(arr[i-1]+prevDiff);
    }

- Amit Petkar March 12, 2014 | 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