WalzoneInterview Prep
πŸ“ž Interviewing soon? Practice with a realistic AI mock phone interview β€” it calls you, then scores you. First 15 min FREE β†’

Wall Street Quant Β· Puzzles & Problems Β· question 40 of 155

You are given an array of integers from 1 to 100, and two numbers are missing. How can you efficiently find the two missing numbers?

πŸ“• Buy this interview preparation book: 155 Wall Street Quant questions & answers β€” PDF + EPUB for $5

Let the two missing numbers be x and y. To find them efficiently, we can use the following procedure:

1. Calculate the sum and the sum of squares of the missing numbers:

Since the array contains integers from 1 to 100, the sum of the numbers in the complete array, S1, can be calculated using the formula for the sum of an arithmetic series:


$$S_1 = \frac{n(n+1)}{2} = \frac{100 \times 101}{2} = 5050$$

Calculate the sum of the given numbers in the array, s1, and then the sum of the missing numbers:


xβ€…+β€…y = S1β€…βˆ’β€…s1 = 5050β€…βˆ’β€…s1

Now let’s calculate the sum of the squares of the integers in the complete array, S2:


$$S_2 = \sum_{i=1}^{100} i^2 = \frac{n(n+1)(2n+1)}{6} = \frac{100 \times 101 \times 201}{6} = 338350$$

Compute the sum of the squares of the given numbers in the array, s2, and the sum of the squares of the missing numbers:


x2β€…+β€…y2 = S2β€…βˆ’β€…s2 = 338350β€…βˆ’β€…s2

2. Solve the system of linear equations:

Now, we have two equations with two variables (x and y):


$$\begin{cases} x + y = 5050 - s_1 \\ x^2 + y^2 = 338350 - s_2 \\ \end{cases}$$

We can solve this system of equations by first squaring the first equation:


(xβ€…+β€…y)2 = (5050β€…βˆ’β€…s1)2

Expanding and rearranging, we get:


2xy = (5050β€…βˆ’β€…s1)2β€…βˆ’β€…(338350β€…βˆ’β€…s2)

Next, we can solve for one of the variables, say y, and substitute it into the second equation:


$$y = \frac{(5050 - s_1)^2 - (338350 - s_2)}{2x}$$

Substituting this into the first equation, we get a quadratic equation in x:


$$x + \frac{(5050 - s_1)^2 - (338350 - s_2)}{2x} = 5050 - s_1$$

3. Solve the quadratic equation:

At this point, one can solve this quadratic equation in x using standard methods such as the quadratic formula, factoring, or numerical methods. After finding the value of x, substitute it back into the equation for y to find the corresponding value of y. These will be the two missing numbers.

It’s worth noting that this method might not be the most practical in terms of computational complexity. If computational efficiency is essential, we can use XOR bitwise operation to solve this problem. However, the mathematical steps described above are useful for understanding the logic behind solving this problem.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Wall Street Quant interview β€” then scores it.
πŸ“ž Practice Wall Street Quant β€” free 15 min
πŸ“• Buy this interview preparation book: 155 Wall Street Quant questions & answers β€” PDF + EPUB for $5

All 155 Wall Street Quant questions Β· All topics