Count Number of Ways to Place Houses - Problem

There is a street with n * 2 plots, where there are n plots on each side of the street. The plots on each side are numbered from 1 to n. On each plot, a house can be placed.

Return the number of ways houses can be placed such that no two houses are adjacent to each other on the same side of the street. Since the answer may be very large, return it modulo 109 + 7.

Note: If a house is placed on the ith plot on one side of the street, a house can also be placed on the ith plot on the other side of the street.

Input & Output

Example 1 — Small Street
$ Input: n = 2
Output: 9
💡 Note: For each side with 2 plots, there are 3 ways: [], [1], [2]. Total combinations: 3 × 3 = 9 ways to arrange houses on both sides.
Example 2 — Single Plot
$ Input: n = 1
Output: 4
💡 Note: For each side with 1 plot, there are 2 ways: place house or don't place. Total: 2 × 2 = 4 ways for both sides.
Example 3 — Larger Street
$ Input: n = 3
Output: 25
💡 Note: For each side with 3 plots, there are 5 valid arrangements. Both sides independent: 5 × 5 = 25 total ways.

Constraints

  • 1 ≤ n ≤ 1000

Visualization

Tap to expand
Count Ways to Place Houses: Two-Sided Street (n=3)North Side123STREET123South SideValid arrangements for one side:[ ] [ ] [ ][H] [ ] [ ][ ] [ ] [H][ ] [H] [ ][H] [ ] [H]5 ways per sideBoth sides independentResult: 5 × 5 = 25 total ways
Understanding the Visualization
1
Input
Street with n plots on each side (n=3)
2
Rule
No adjacent houses on same side allowed
3
Output
Count all valid arrangements (25 ways)
Key Takeaway
🎯 Key Insight: Each side follows Fibonacci pattern, both sides are independent - multiply the results
Asked in
Google 25 Amazon 20 Microsoft 15 Apple 12
23.4K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen