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

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for 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.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my