Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Convert Number to Binary

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Convert Number to Binary. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Technogen India Pvt. needs a utility that formats numeric values for low-level processing. Given a nonnegative integer, return its binary representation as a string without leading zeroes.

Implement the function to_binary(number).

Formal Specification

  • Input: number, an integer where 0 <= number <= 10^9.
  • Output: A string containing only 0 and 1.
  • The output must represent number in base 2.
  • The value 0 must return "0".
  • Do not use Python's bin() function or other direct base-conversion utilities.

Constraints

  • 0 <= number <= 10^9
  • The input is always an integer
  • The output contains only '0' and '1'
  • The output has no leading zeroes unless the value is zero

Function Signature

def to_binary(number):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output