Which ball is missing? Photo credits: 90% wikiHow, 9% photopea.com 1% me

For an on-site tech interview at startup company XYZ, I was asked the following question:

You are given an array of 99 integers. It is missing exactly one integer from a range of 1 to 100 and is unsorted. Write up a function findMissingNumber(arr) to find the missing integer.

The interviewer (aside: he was very friendly) then asked me to solve the problem in any way that I could without worrying about efficiency in mind.

How I solved it

I suggested that the first thing I would do then, is to sort my array in ascending order, then write a for-loop from 1 to 100, then as soon as the index of the for-loop didn’t match the value of my array at that given index minus 1, I would return that index.

I ended up writing the following down (after correcting those pesky off-by-one errors with <= vs. < and i = 0 vs i = 1 )

const findMissingNumber = (arrayWithMissingInteger) => {

const sortedArr = arrayWithMissingInteger.sort((a, b) => {

return a - b;

}

);



for (let i = 1; i <= 100; i++) {

if (sortedArr[i - 1] !== i) {

return i;

}

}

}

He then said things look good, then asked me to analyze the runtime. I then told him that since I sorted it, my runtime is now at O(n log n) .

The cost of sorting an array as O(n) jumps to O(n log n) http://bigocheatsheet.com/

He then proceeded to tell me that there is a way to get the runtime down to O(n) and then I got nervous. Then he gave me a hint that I should try ‘summing stuff up.’ After some bantering, I decided to solve a smaller version of the problem and see if I could scale it up.

I wrote on the whiteboard something along the following:

const arrayWithAllNumbers = [1, 2, 3]; const arrayWithMissingNumber = [3, 1]; (1 + 2 + 3) - (3 + 1) = 2

I blew a sigh of relief then told him that I would just sum up all the numbers from 1 to 100, then subtract that amount with the sum of my arrayWithMissingNumber to get the answer.

He did not ask me to write up the code, as he seemed to think that I got the big picture, but here’s what I would have written down:

const findMissingNumber = (arrayWithMissingInteger) => {

let sum = 0;

for (let i = 1; i <= 100; i++) { sum += i }



let missingIntSum = 0;

for (int of arrayWithMissingInteger) { missingIntSum += int } return sum - missingIntSum;

}

He did however, gently prod me on how I would figure out the sum from 1 to 100. I retorted that I guess I would just use a for-loop like above, and he politely smiled (foreshadowing!) and we proceeded to solve the next question. I drove home a few hours later feeling great about that question. I showed that man grit and heart dammit!

How I should’ve solved it: triangular series

I got home, then got out of my recently bought business-casual interview attire, decompressed for a bit, and googled the problem, and proceeded to learn about the triangular series.

It turns out that if we have a series of integers that starts with 1 and increases by 1 with each number, and we know what the last number n is, the sum of the series reduces down to: ∑ = (n^2 + n) / 2

Oh…I gotta blog about this.

This makes for a cool magic trick at a party.

[1, 2, 3, 4, 5] 1 + 2 + 3 + 4 + 5 = 15 (5^2 + 5) / 2 = 15 ========================= [1, 2, 3 ... 100] 1 + 2 + ... 99 + 100 = ?? (100^2 + 100) / 2 = 5050

Had I known this, (or miraculously had I remembered that one lecture in Ms. Hughes’ 8th grade geometry class) I would have written something like the following on the whiteboard:

const findMissingNumber = (arrayWithMissingInteger) => {

let sum = (Math.pow(100, 2) + 100) / 2; for (int of arrayWithMissingInteger) { sum -= int } return sum;

}

This lets me iterate through just one array of 99 integers, as opposed to iterating through two arrays of 100 integers and 99 integers to get the missing number.

Closing Thoughts

Overall, answering this question was a moral victory for me. The interviewer and I built a friendly rapport where it felt more like a pair-programming session where we gradually optimized the solution together, and I showed him that I could solve a problem by stubbing out a smaller version of that problem.

Not knowing the triangular series off the top of my head, I can accept that and just move on. On the plus side, if I ever find myself playing pool and find out that exactly one ball is missing from the triangle rack, I will know exactly what to do.

Thanks for reading! Please feel free to leave a few claps down below!