Generate Binary Numbers Using a Queue

Given n, return an ordered array of strings containing the canonical binary representations of the integers 1 through n, in increasing numeric order. Strings…

Given n, return an ordered array of strings containing the canonical binary representations of the integers 1 through n, in increasing numeric order. Strings contain only 0 and 1, have no leading zero and no 0b prefix. Do not return numeric values or lexicographically sort the strings. A queue of prefixes is one solution; any correct method is accepted.

1 <= n <= 1000; n is a strict integer, not a Boolean or fraction. Return exactly n strings, corresponding in order to 1,2,...,n. Do not include zero, leading zeros, numeric array items or a binary prefix. Teaching traces use 1 <= n <= 12; coding retains n <= 1000 and the complete output.

Example 1: input n = 3; output ["1","10","11"]. The output strings represent numeric values 1, 2 and 3.

Example 2: input n = 8; output ["1","10","11","100","101","110","111","1000"]. Numeric order crosses from three-bit 111 to four-bit 1000; this is not lexicographic order.

Category: Queues. Topic: queue. Difficulty: Easy. Patterns: linear-scan.