## Model N Interview Question for Applications Developers

• 0

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

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

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...

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

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

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

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

min = numbs
max = numbs
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``````

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

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

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

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

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

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

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

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;
int largestNumber = smallestNumber;

for (int eachNumber : intArray) {

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;

}

}``````

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

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

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

Array's not sorted.

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.

### 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.