KatInterview

Integer Sequence Rule Inference and Next-Term Prediction

Math & Bit ManipulationModel answer
MediumModel answerMath & Bit Manipulationpattern-recognitionmodel-selectionfinite-differences

Last updated

Design a systematic program that detects the governing rule of an integer sequence and predicts the next k terms, ranked by plausibility.

Integer Sequence Rule Inference and Next-Term Prediction

You receive an integer sequence of between 2 and 50 terms. The sequence may have been generated by any of the following mechanisms:

  • A polynomial rule (including constant, arithmetic, and higher-degree progressions)
  • A linear recurrence, possibly with a constant offset (e.g. geometric progressions, Fibonacci-like rules, affine maps)
  • A digit-level transform applied repeatedly (e.g. each term equals the previous term plus its own digit sum)
  • An interleaving of two or more independent subsequences (e.g. odd-indexed and even-indexed terms each follow their own rule)
  • A combination or nesting of the above

Your task is to describe a complete, systematic strategy for inferring the rule and predicting the next k terms (1 ≤ k ≤ 5). Then implement that strategy as a program.

What your answer should cover

  1. Heuristic pipeline — which rule families you check, in what order, and why that order makes sense
  2. Model selection — how you score competing explanations and choose among them when more than one fits the observed terms
  3. Insufficient evidence and ambiguity — how you detect and report cases where the sequence is too short to confirm any rule, or where two rules with different predictions are equally plausible
  4. Implementation — working code that accepts the sequence and k, and outputs candidate rules with their predicted next terms and plausibility scores
  5. Complexity analysis — time and space cost of each stage
  6. Edge cases — n = 2, constant sequences, non-integer intermediate values, negative terms, very fast growth

Input / output

Input:  a list of integers  s  (2 ≤ len(s) ≤ 50)
        an integer          k  (1 ≤ k ≤ 5)

Output: for each distinct prediction of the next k terms,
        the best-scoring rule that produces it and its score;
        or a message indicating insufficient evidence / ambiguity

Example

s = [2, 5, 8, 11, 14],  k = 3
Expected output: arithmetic (d=3), next terms: [17, 20, 23], score: 3.5
s = [1, 2, 4, 8, 16],   k = 2
Expected output: geometric (r=2), next terms: [32, 64], score: 3.5
         (flag: degree-4 polynomial predicts [31, 57] with score ≤ 0 — ambiguity note)
s = [1, 2],             k = 1
Expected output: insufficient evidence

Hints

Hint 1

Start with finite differences: repeatedly subtract consecutive terms. If any row becomes constant, you have a polynomial of that degree. This check is O(n²) and catches arithmetic, quadratic, and higher-degree rules before you try anything more expensive.

Hint 2

Model selection is cleaner when you separate fitting from verification. Fit a rule's parameters on the smallest prefix needed (e.g. d+1 terms for a degree-d polynomial), then count how many of the remaining terms it predicts exactly. That count is your evidence; penalize it by the number of free parameters to get a score.

Hint 3

Interleaving patterns are easy to miss. Before giving up, split the sequence into even-indexed and odd-indexed subsequences and run your full pipeline on each part independently. This handles alternating rules like +3, ×2, +3, ×2 without any special-case logic.