Count Valid Infection Orders
Company: Uber
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates combinatorial enumeration and algorithmic reasoning by counting valid sequences of state changes in a linear infection process. It is commonly asked in technical interviews because it measures the ability to model propagation constraints, count permutations under adjacency restrictions, and implement efficient solutions within the Coding & Algorithms domain, focusing on practical application rather than purely conceptual understanding.
Constraints
- 1 <= n <= 100000
- 1 <= len(infected) <= n
- 0 <= infected[i] < n
- infected is sorted in strictly increasing order
- Use arbitrary-precision arithmetic if needed, since the answer can be large
Examples
Input: (5, [0, 4])
Expected Output: 4
Explanation: There is one internal gap of length 3: houses [1, 2, 3]. At each of the first 2 steps, you can infect from the left end or the right end of that gap, so the total is 2^(3-1) = 4.
Input: (5, [2])
Expected Output: 6
Explanation: The left segment [1, 0] has a forced local order, and the right segment [3, 4] also has a forced local order. Interleaving these two length-2 sequences gives 4! / (2! * 2!) = 6.