Startup / VO Interview Question: Generate All Binary Strings Without Adjacent 0s

15 Views
No Comments

Please implement a Python function that takes a non-negative integer argument n as input, and it produces a list of all binary strings with length n such that none of the strings in the output list contains adjacent 0s.

generate_strings(0) --> ['']
generate_strings(1) --> ['0', '1']
generate_strings(2) --> ['01', '10', '11']
generate_strings(3) --> ['010', '011', '101', '110', '111']

This problem asks for all binary strings of length n with no adjacent 0s. A natural solution is backtracking or DFS: build the string one character at a time, and only append 0 when the previous character is not 0. The sample outputs for n = 0, 1, 2, and 3 clarify the base case and the expected generation pattern. The key is to prune invalid branches early so the recursion only explores valid strings.

END
 0