Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Whiteboard: Binary to String

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

Your question is Whiteboard: Binary to String. 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

A Gentex embedded controller must encode signed sensor values into fixed-width binary fields before transmission. Implement a function that converts an integer to its binary representation without using Python's bin() or formatting helpers.

The result must use two's-complement representation and contain exactly bits characters. Positive values are left-padded with zeroes. Negative values are represented by adding 2^bits before extracting bits.

Formal Specification

Implement integer_to_binary(value, bits).

  • Input: value, an integer in the signed range -2^(bits-1) <= value < 2^(bits-1), and bits, an integer from 1 through 32.
  • Output: a string of exactly bits characters, each character either '0' or '1'.
  • Do not call bin(), format(), string conversion of the integer, or library-based base conversion.

Constraints

  • 1 <= bits <= 32
  • -2^(bits - 1) <= value < 2^(bits - 1)
  • The output contains exactly bits characters
  • Only '0' and '1' may appear in the output
  • Built-in binary conversion and formatting helpers are not allowed

Function Signature

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