Created
December 17, 2019 09:17
-
-
Save Pierre-M/684ef0deff8e38bfcca33215e8127803 to your computer and use it in GitHub Desktop.
Interview question by Cassidy Williams (2019-12-16 newsletter)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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