# Count The Number Of Infection Sequences: Study Guide

Count The Number Of Infection Sequences shows up often in coding rounds. Here is how I talk through it when the interviewer is watching.

## Key idea

A line of n houses; some start infected. Each second an infected house infects a neighbor. The number of orders the originally-uninfected houses can become infected equals, for each gap between two initially infected houses, 2^{gap-1} ways to eat the gap from both ends, times a multinomial over gap sizes (they interleave). Ends that touch only one infected house have only 1 order.

## Steps to say out loud

1. Restate the input and the exact ask, and nail down constraints and overflow.
2. Name the pattern (binary search on the answer, monotonic stack or deque, interval / interval-DP, string hashing, graph traversal, or shortest paths) and why a nested scan or naive search fails.
3. Code it, then trace a short example plus one empty or already-solved edge case.

## Complexity

O(n + gaps).

## Common mistake

Treating every uninfected house as independently orderable (n!) without the two-end constraint.

## Practice tip

Time yourself: two minutes to explain, ten to code, three to test edges. Disclosure: I work on Linkjob AI. I also keep a practice cheat sheet at official website.