Easy interview question got harder: given numbers 1..100, find the missing number(s) given exactly k are missing


Easy Interview Question Got Harder: Finding Missing Numbers
š Introduction: I had a job interview where I was asked a seemingly simple question about finding a missing number from a bag of numbers. However, the question quickly became more challenging when multiple numbers were missing. In this blog post, I will share the original question, my initial solution, and then explore how to solve it when two or more numbers are missing. Let's dive in!
The Original Question
The original question was as follows:
Q1: We have a bag containing numbers 1, 2, 3, ā¦, 100. Each number appears exactly once, so there are 100 numbers. Now one number is randomly picked out of the bag. Find the missing number.
My initial answer (A1) was to use the sum of an arithmetic series formula to find the missing number. The sum of numbers from 1 to N can be calculated using the formula (N+1)(N/2). For N = 100, the sum is 5050. By subtracting the sum of the numbers in the bag from 5050, we can find the missing number.
The Unexpected Twist
However, the second question (Q2) took me by surprise:
Q2: That is correct, but now how would you do this if TWO numbers are missing?
I hadn't seen this variation before, so I struggled to find a solution. I threw out some ideas like comparing against the expected product or doing a second pass, but I didn't have a clear path to the solution.
The Generalized Technique
To my surprise, the interviewer revealed that the technique could be generalized to find k missing numbers, not just two. This sparked my curiosity, but I couldn't figure out the technique even months after the interview. So, here we are, looking for the elusive solution.
Qk: If exactly k numbers are missing from the bag, how would you efficiently find them?
Let's break down the questions and find some answers!
š How to Solve Q2?
To solve Q2, where two numbers are missing, we need to gather more information than just the sum. One possible approach is to calculate the product of the numbers before subtracting it from the expected product. By finding the difference between both the sum and the product, we can find the two missing numbers.
š How to Solve Q3?
Extending this approach, we can find three missing numbers using the same principle. The sum, product, and another calculation called "sum of squares" can help us identify the missing numbers. By taking into account these three calculations and their differences, we can solve for the three missing numbers.
š How to Solve Qk?
Generalizing the technique, we can find k missing numbers efficiently. By calculating the kth power sum, product, and other k-specific calculations, we can determine the k missing numbers.
š” Conclusion:
Even though this interview question seemed simple at first, it became progressively harder as more numbers were missing. However, by leveraging calculations like the sum, product, and other k-specific calculations, we can efficiently find missing numbers. Keep practicing and exploring different techniques to sharpen your problem-solving skills.
Now it's your turn! How would you solve Q2, Q3, or Qk? Share your ideas and techniques in the comments below. Let's master this challenging interview question together!
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.
