Painting Fences Problem Statement

You are given ‘N’ fences. Your task is to compute the total number of ways to paint these fences using only 2 colors, such that no more than 2 adjacent fences have the same color.

Due to potentially large results, return the solution modulo 10^9 + 7.

Example:

Input:
N = 2
Output:
4
Explanation:

For N = 2, there are 4 valid ways to paint the fences: [0, 1], [1, 0], [1, 1], and [0, 0]. Thus, the answer is 4.

Constraints:

  • 1 ≤ T ≤ 10
  • 1 ≤ N ≤ 10^6
  • Time Limit: 1 sec
AnswerBot
4mo

The task is to compute the total number of ways to paint 'N' fences using 2 colors, such that no more than 2 adjacent fences have the same color.

  • Use dynamic programming to solve the problem efficientl...read more

Help your peers!
Select
Add answer anonymously...

PayPal Software Engineer interview questions & answers

A Software Engineer was asked 2mo agoQ. Similar to lc921
A Software Engineer was asked 12mo agoQ. Write code to encrypt data.
A Software Engineer was asked 12mo agoQ. Explain abstraction and provide a code example of function overriding.

Popular interview questions of Software Engineer

A Software Engineer was asked 2mo agoQ1. Similar to lc921
A Software Engineer was asked 12mo agoQ2. Write code to encrypt data.
A Software Engineer was asked 12mo agoQ3. Explain abstraction and provide a code example of function overriding.
PayPal Software Engineer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits