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

Coding Interview Essentials Β· Array and String Problems Β· question 36 of 120

How would you find the missing number in a sequence?

πŸ“• Buy this interview preparation book: 120 Coding Interview Essentials questions & answers β€” PDF + EPUB for $5

There are several types of sequences you might encounter, including arithmetic sequences, geometric sequences, and more complex series. The method for finding the missing number will depend on the type of series. Let’s walk through methods for both arithmetic and geometric sequences as examples.

1. **Arithmetic sequences**: In an arithmetic sequence, the difference between consecutive terms is the same. If you’re given such sequence and there is a missing term, you can use the formula for the nth term of an arithmetic sequence to find it:


a_n = a_1β€…+β€…(nβ€…βˆ’β€…1)β€…*β€…d

where an is the nth term, a1 is the first term, n is the position of the term in the sequence, and d is the common difference.

For example, if you have a sequence 4, _, 8, you know that d = 2 because the difference between 8 and 4 is 4. The missing term is the second term, so n = 2. Plugging in these values:


a_2 = 4β€…+β€…(2β€…βˆ’β€…1)β€…*β€…2 = 6

So 6 is the missing number.

2. **Geometric sequences**: In a geometric sequence, the ratio between consecutive terms is the same. If there is a missing number, you can use the formula for the nth term of a geometric sequence to find it:


a_n = a_1β€…*β€…r(nβ€…βˆ’β€…1)

where an is the nth term, a1 is the first term, n is the position of the term in the sequence, and r is the common ratio.

For example, in a sequence 2, _, 8, you can see that the common ratio is 4. The missing number is the second term, so n = 2. Plugging in these values:


a_2 = 2β€…*β€…4(2β€…βˆ’β€…1) = 8

However, this result contradicts our sequence, so we may have assumed the wrong ratio. Since geometric sequences multiply by a common ratio, it’s quite possible that our ratio is, in fact, the square root of 4, which is 2. Let’s try that:


a_2 = 2β€…*β€…2(2β€…βˆ’β€…1) = 4

And so, 4 is the missing number in the geometric sequence.

Remember, these are just two examples β€” if you have a more complex series or other types of number sequences, there may be different rules to identify and deal with them.

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

All 120 Coding Interview Essentials questions Β· All topics