Find missing numbers in sequences
Company: Optiver
Role: Data Scientist
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Take-home Project
Quick Answer: This question evaluates sequence pattern recognition, numerical reasoning with integers and rational numbers, and the ability to infer recurrences or closed-form relations from series data.
Part 1: Number Logic - Fill the Missing Term
Constraints
- 4 <= len(sequence) <= 30
- Exactly one entry is '?'
- The full sequence matches exactly one supported family
- If the sequence is geometric, all known terms are non-zero and the ratio is rational
Examples
Input: ['3', '7', '11', '?', '19']
Expected Output: "15"
Explanation: This is an arithmetic progression with common difference 4.
Input: ['16', '8', '4', '?', '1']
Expected Output: "2"
Explanation: This is a geometric progression with ratio 1/2.
Hints
- Use exact rational arithmetic instead of floating point to avoid precision mistakes.
- Try inferring the missing value under each allowed pattern, then verify it against every known position.
Part 2: Beat the Odds - Count Odd-Heavy Subarrays
Constraints
- 0 <= len(nums) <= 200000
- -10^9 <= nums[i] <= 10^9
- The answer can be larger than 32-bit integer range
- An odd number contributes +1 and an even number contributes -1 to a subarray balance
Examples
Input: [1, 2, 3]
Expected Output: 3
Explanation: Valid subarrays are [1], [1,2,3], and [3].
Input: [2, 4, 6]
Expected Output: 0
Explanation: Every subarray has at least as many evens as odds.
Hints
- Convert the problem into counting subarrays with positive sum after mapping odd -> +1 and even -> -1.
- For each prefix sum, count how many previous prefix sums are strictly smaller. A Fenwick tree or balanced BST can help.
Part 3: Likelihood Test - Rank Targets in a Random Walk
Constraints
- 1 <= n <= 200
- 0 <= len(edges) <= 2000
- 0 <= start < n
- 0 <= k <= 200
- Targets are distinct node labels in the range [0, n-1]
Examples
Input: (4, [(0, 1), (0, 2), (1, 2), (2, 2), (2, 3)], 0, 2, [1, 2, 3])
Expected Output: [2, 3, 1]
Explanation: After 2 steps, node 2 has probability 3/4, node 3 has 1/4, and node 1 has 0.
Input: (3, [(0, 1), (1, 2)], 1, 0, [0, 1, 2])
Expected Output: [1, 0, 2]
Explanation: Edge case: with 0 steps, the walker is certainly still at the start node 1.
Hints
- Use dynamic programming: keep the probability distribution over nodes after each step.
- Treat a sink node as having an implicit self-loop with probability 1.