EASY
DATA STRUCTURES AND ALGORITHMS

How to Solve Infinite Binary Print

Written By
Adam Bhula

Infinite Binary Print Introduction

The Infinite Binary Print problem asks us to print out all numbers in binary, preserving leading zeros. The output is not a simple binary sequence but will include all possible binary numbers. This problem requires consideration of how to handle outputting our values when our solution will run infinitely. The usage of generators is vital in solving this efficiently.

Infinite Binary Print Problem

Print out all numbers in binary, preserving leading zeros. The idea is that our output will inevitably contain all possible binary numbers.

The output should be similar to this:

0 1 00 01 10 11 000 001

And so on

Infinite Binary Print Solutions

To solve this problem, we use a queue-based approach. The main idea is to create a queue data structure to store the binary strings. We initialize the queue with the initial strings "0" and "1". In each iteration of the loop, we dequeue a binary string from the front of the queue, print it to display the number, and then generate the next binary strings by appending "0" and "1" to the retrieved string. The newly generated strings are then enqueued back to the end of the queue. This process repeats indefinitely, generating an infinite sequence of binary numbers. By implementing the binary print logic using a generator function, we can generate binary strings indefinitely without needing to pre-compute and store all the values.

One key thing to note when tackling this problem is to remember to print out the values as they are being removed from the queue. A common mistake interviewees make is trying to print all the values at the end. As a result the solution will never print as it is running infinitely and never reaches the end.

def generate_infinite_binary():
    queue = ['0', '1']
    while queue:
        binary = queue.pop(0)
        yield binary
        queue.append(binary + '0')
        queue.append(binary + '1')

def print_infinite_binary():
    for binary in generate_infinite_binary():
        print(binary)

print_infinite_binary()

Time/Space Complexity Analysis

  • Time Complexity: O(n), where n is the number of binary numbers we want to print. In each iteration, we dequeue a string from the queue and enqueue two new strings, so the size of the queue doubles. Since the number of iterations is proportional to the number of binary numbers we want to print, the time complexity is linear.
  • Space Complexity: O(2^n), where n is the number of binary numbers we want to print. This is because the queue can potentially store up to 2^n binary strings, which is the total number of binary numbers that can be generated.

For the purposes of our Java solution we use a Linkedlist class to implement our queue data-structure however this is not required. Alternatively ArrayDequeue may be slightly more space efficient. This depends on other factors such as the specific requirements of the problem, the expected size of the deque, and the operations that need to be performed. It would be important to take note of this in a real interview if questioned by an interviewer.

About interviewing.io

interviewing.io is a mock interview practice platform. We've hosted over 100K mock interviews, conducted by senior engineers from FAANG & other top companies. We've drawn on data from these interviews to bring you the best interview prep resource on the web.

We know exactly what to do and say to get the company, title, and salary you want.

Interview prep and job hunting are chaos and pain. We can help. Really.