Skip to content

Instantly share code, notes, and snippets.

@Pierre-M
Created December 17, 2019 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pierre-M/684ef0deff8e38bfcca33215e8127803 to your computer and use it in GitHub Desktop.
Save Pierre-M/684ef0deff8e38bfcca33215e8127803 to your computer and use it in GitHub Desktop.
Interview question by Cassidy Williams (2019-12-16 newsletter)
/*
Given a positive number N, generate binary numbers between 1 to N
using a queue-type structure in linear time.
*/
function binaryQueue(x) {
return [...Array(x).keys()] // let's build an array of numbers from 0 to x
.slice(1) // drop the first element to have an array from 1 to x
.map(k => k.toString(2)) // now we have an array of binaries from 1 to x as strings
.map(e => parseInt(e, 10)) // now we have an array of numbers from 1 to x in binaries
.join(" ") // let's have the final touch :)
}
console.log(binaryQueue(10)); // here is a simple exemple
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment