Count Ordered Sequences That Sum to a Target
Company: Optiver
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: Given a positive target n and a set of unique positive order sizes, count the distinct ordered sequences of any length whose elements sum exactly to n. Work through the function contract, boundary cases, correctness argument, and time and space complexity expected in a production-quality solution.
Constraints
- 1 <= n <= 10000; 1 <= len(sizes) <= 100.
- Every size is unique and lies from 1 through n; sizes may be reused in a sequence.
- Sequence order matters and the exact answer is at most 2^53 - 1.
Examples
Input: (1, [1])
Expected Output: 1
Explanation: The only sequence is one occurrence of size one.
Input: (3, [1, 2])
Expected Output: 3
Explanation: The valid ordered sequences are 1+1+1, 1+2, and 2+1.
Hints
- For sizes 1 and 2, compare the two different orders that reach target 3.
- Test one size equal to the target and a target unreachable from all allowed sizes.
- Include the maximum target with many large allowed sizes and a wide but exact result case.