Community

Start a Thread


Notifications
Subscribe You’re not receiving notifications from this thread.

Decimal To Binary (Main Thread)

Here is the interview question prompt, presented for reference.

Today, we're going to do some math! There is only one prerequisite for answering this question, and it is knowing the definitions of binary and decimal.

According to How to Convert Decimal to Binary and Binary to Decimal, binary is a numeric system that only uses two digits — 0 and 1. Computers operate in binary, meaning they store data and perform calculations using only zeros and ones.

Decimal, on the other hand, is "a number system that uses a notation in which each number is expressed in base 10 by using one of the first nine integers or 0 in each place and letting each place value be a power of 10."

Can you write a function that returns the binary string of a given decimal number? For example, if the input was 3:

decimalToBinary(3);
// 11

We get 11 because it is the binary representation of 3 (1 x 2 + 1 x 1).

For more background on this, read this great article on converting: How to Convert Decimal to Binary and Binary to Decimal.

Constraints

  • The given number will be a positive integer in the range between 0 and 1000000000
  • Expected time complexity : O(log n)
  • Expected space complexity : O(1)

You can see the full challenge with visuals at this link.

Challenges • Asked almost 2 years ago by Anonymous

Jake from AlgoDaily Commented on Aug 28, 2019:

This is the main discussion thread generated for Decimal To Binary.

Anonymous Commented on Aug 28, 2019:

Hi, I appreciate that you've backlinked to my article, but since you've used a fragment of it, use the title in the text of the backlink. I.e. "How to Convert Decimal to Binary and Binary to Decimal".

Anonymous Commented on Aug 28, 2019:

How to Convert Decimal to Binary and Binary to Decimal
by Eugene Brennan

https://owlcation.com/stem/How-to-Convert-Decimal-to-Binary-and-Binary-to-Decimal

Rahat Zaman Commented on Feb 26, 2021:

Thanks for your comment. We updated the article and added your citation properly.