Skip to main content
All Questions

Count the distinct sequences in which all houses in a line become infected.

Unlock detailed company stats for this questionUpgrade

Given n houses numbered 0 to n-1 in a straight line and an array infected of houses infected on Day 0, every infected house infects its immediate neighbors (i-1 and i+1) starting the next day. Infection continues until every house that can be infected is infected. Two outcomes are different if the sequence in which houses become infected differs. Return the number of distinct possible infection sequences, modulo 10^9 + 7.

Example 1: n = 5, infected = [2]. Day 0: 2; Day 1: 1, 3; Day 2: 0, 4. Houses on the same day can appear in either order, giving [2,1,3,0,4], [2,1,3,4,0], [2,3,1,0,4], [2,3,1,4,0]. Answer: 4.

Example 2: n = 6, infected = [2, 4]. The infection spreads simultaneously from both sources; count all distinct valid sequences.

The code had to run and pass all test cases on HackerRank.

Also asked as · Count the distinct infection sequences across a line of houses.

Community answers

No answers contributed by the community yet.

Related courses

Course

Software Engineering Interview Prep

Land your dream software engineering role at Google, Amazon, Microsoft, Meta, Apple, and other top companies. Learn from mock interviews, frameworks, and advice from senior candidates—practice data structures, algorithms, system design, people management, behavioral interviews, and more.

Course

System Design Interviews

Learn how to answer the latest system design questions across product, infrastructure, and AI domains. Features in-depth video examples, written breakdowns, and helpful reference patterns. Watch senior engineers and managers answer these questions in mock interview videos.