Model N Interview Question for Applications Developers


Country: United States
Interview Type: Phone Interview




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

if i understand your question correctly. the logic should be something like this
1. Add all +ve integers let it be S
2. Let N be the total number of elements in the array. Missing number should be
N*(N+1)/2 - S

It works with the sample inputs you provided

- skp September 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

N should be the maximum positive number in the array not the number of elements in the array.

also... if N*(N+1)/2 == S, then the number is N+1;

for example.. in the array, 3, 4, 2, 1, the answer is 5...

- JustCoding September 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think question is not that simple as it seems. The above algorithm would fail for input like
[1,4,5,-1,0]

- skp September 28, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

numbs = [1,2,4,5,-1,0]


min = numbs[0]
max = numbs[0]
missing = -1

for n in numbs[1:]:
    if (n <= 0):
        continue
    if (n < min):
        if(n+1 < min):
            missing = n+1
        else:
            min = n

    if (n > max and missing == -1):
        if(max+1 < n):
            missing = max+1
        max = n

if missing == -1:
    missing = max+1

print missing

- bluesky September 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Wrong when using a[] = {2,3}

- GKalchev October 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

how come from [2,1,0] ---> 3 is missing ?

- Anonymous September 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

sorting the list ->[0,1,2]
the first positive missing element is 3.

- AC Srinivas October 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sort the array

for i to n
if a[i] < 0 continue
else
if a[i + 1] - a[i] != 1
print a[i] + 1
break

- himanshu October 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Of course this will only work if only one number is missing from the sequence.
This solution works for any sequence not just the ones provided
(utilizes N*(N+1)/2)

public class Main {

	public static void main(String[] args) {

		int[] unsorted = { 15,17,18,20,13,21,19,14 };

		findMissing(unsorted);

	}

	static void findMissing(int[] intArray) {
		//can be any numbeer in the array
		int smallestNumber = intArray[0];
		int largestNumber = smallestNumber;
		
		int arrayAddition = 0;

		for (int eachNumber : intArray) {
			arrayAddition += eachNumber;
			
			if (eachNumber < smallestNumber)//get the smallest number
				smallestNumber = eachNumber;
			else if(eachNumber > largestNumber)//get the largest number
				largestNumber = eachNumber;
		}
		
		int ZeroToLargest = ((largestNumber) * (largestNumber + 1))/2;
		int ZeroToSmallest = ((smallestNumber - 1) * (smallestNumber))/2;

		System.out.println(ZeroToLargest - ZeroToSmallest - arrayAddition);
	}

}

- Dimitri Morgan July 19, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

findMissing(array a){
        int tmp = a[0];
        foreach(int i = 1 ; i<a.lenght; i++){
           if(++temp!=a[i]){
               return ++temp;
           }
        }
     }

- kamoliddin September 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Array's not sorted.

- Anonymous September 27, 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