Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Decimal to Binary Coding

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

Your question is Decimal to Binary Coding. 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

In a Birlasoft QA automation utility, numeric values may need to be represented in binary for validation and low-level test checks. Given a non-negative decimal integer, return its binary representation as a string without using Python's built-in base-conversion functions such as bin().

Formal Specification

Implement decimal_to_binary(n), where n is an integer greater than or equal to zero. Return a string containing the binary digits of n, with no leading zeroes except that zero itself must return "0".

Use repeated division by two: the remainder from each division is the next binary digit, starting from the least significant digit. Reverse the collected remainders before returning the result.

Constraints

  • 0 <= n <= 2^31 - 1
  • The input is an integer.
  • Do not use built-in base-conversion functions.
  • Return a string with no leading zeroes except for "0".

Function Signature

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