Decimal To Binary (Medium)

Good afternoon! Here's our prompt for today.

You may see this problem at Facebook, Twitter, Atlassian, Snap, Jane Street, Visa, Asana, Cockroach Labs, Cohesity, Bosch Global, Zoho, and Sony.

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."

Description

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

JAVASCRIPT
1decimalToBinary(3);
2// 11

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

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)
JAVASCRIPT
OUTPUT
Results will appear here.