Prepin
Log in

Identify all binary strings of N digits that do not contain consecutive 1's.

Approach / Explanation

This question evaluates your skills in addressing issues related to binary strings, along with your understanding of combinatorics, pattern recognition, and either recursion or dynamic programming. Your task is to determine all possible binary strings of length N (where a binary string consists solely of 0s and 1s) that do not contain two consecutive 1s. Another way to state this task is to generate all binary strings of length N that lack "11" as a substring. Key points to consider when answering the question: Comprehend the task's requirement: a binary string without consecutive 1s. Identify the recurrence relation inherent in the problem. This type of issue is typically classified as a classic dynamic programming challenge. Consider binary strings that end in '1' and '0' separately. The total count will be the sum of both categories.

Suggested Answer

A straightforward approach to addressing this problem involves identifying a pattern in smaller strings and progressively constructing the solution for a length N. Here is the base case: When N = 1, the binary strings are: 0 and 1. For N = 2, the binary strings are: 00, 01, 10. [NOT 11, as it includes consecutive 1s.] You can see a pattern emerging; for N = 3, you can take all strings of length 2 that end with 0 and append either a 0 or a 1, and take all strings of length 2 that end with 1 and append a 0. Thus, the strings of length 3 are: 000, 001, 010, 100, 101. By continuing this pattern, we can develop a program that computes the number of strings for smaller values and applies these findings to determine the solution for larger values.

Alternative Answer

Let's analyze binary strings that end with '0' (denoted as a0) and those that end with '1' (denoted as a1). For a binary string of length N, we have: a0[N] = a0[N - 1] + a1[N - 1] a1[N] = a0[N - 1] This means that a binary string of length N that ends in '0' can be formed by adding '0' to all strings of length N-1. Conversely, strings that end in '1' can only be formed from N-1 length strings that end in '0'. Therefore, the total count a[N] for strings of length N would be a0[N] + a1[N]. By applying this reasoning using dynamic programming, you can implement the solution in your preferred programming language. Keep in mind that the base cases are a0[1] = a1[1] = 1 for N=1.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.