Katkat

Find missing numbers in sequences

OptiverData Scientist · Online Assessment

Last updated

This problem tests pattern recognition and algorithm design for identifying missing terms in a sequence that may follow arithmetic, geometric, polynomial, or linear recurrence rules.

Problem Statement

You are given a list of integers seq that contains exactly one missing term, denoted by the value None. Your goal is to identify the underlying mathematical pattern and return the value of the missing term. The sequence is guaranteed to have at least 4 elements.

The sequence could follow one of these patterns:

  1. Arithmetic: Each term increases by a constant d (a_n = a_{n-1} + d).
  2. Geometric: Each term is multiplied by a constant r (a_n = a_{n-1} * r).
  3. Quadratic: The second difference between terms is constant.
  4. Linear Recurrence: Each term is a linear combination of the previous two terms (a_n = c1 * a_{n-1} + c2 * a_{n-2}).

Implement the function: def find_missing_term(seq: List[Optional[int]]) -> int:

Examples

Example 1: Input: [2, 4, 8, None, 32] Output: 16 Explanation: This is a geometric sequence with ratio 2.

Example 2: Input: [1, 4, 9, None, 25] Output: 16 Explanation: This is a sequence of squares (n^2).

Constraints

  • Sequence length: 4 <= len(seq) <= 100.
  • Values: Integers between -10000 and 10000.
  • The missing term will always be an integer.

Edge Cases

  • The None value can appear at the beginning, middle, or end of the list.
  • If multiple patterns fit, prioritize the most restrictive one (e.g., Arithmetic over Recurrence).

Hints

Hint 1

Identify the position of the None value and try to interpolate using neighbors to test for basic arithmetic or geometric differences.

Hint 2

For more complex sequences, compute the first and second-order differences of the known values to see if they form a constant or arithmetic progression.

Hint 3

If the sequence is a linear recurrence, set up a system of equations using the known triplets to solve for the coefficients c1 and c2.

Follow-up questions

  • How would you modify your solution if the input contained floating-point numbers instead of integers?
  • What if the sequence contained two missing values instead of one?
  • How would you optimize the search if the sequence length were to increase to 10^6?