Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Binary XOR Coding
00:00
5 left

Binary XOR Coding

MediumPython

Problem

While validating diagnostic bitmasks for a Western Digital WD_BLACK SN850X device, implement binary XOR directly on strings. Return the result once with an iterative algorithm and once with a recursive algorithm, without converting the complete inputs to integers.

Formal Specification

Implement binary_xor(a, b, recursive=False):

  1. a and b are non-empty strings containing only '0' and '1'.
  2. Align both strings by their rightmost bit, padding the shorter string on the left with zeros.
  3. XOR corresponding bits and return a binary string with no leading zeros, except that zero must be returned as '0'.
  4. When recursive is False, use an iterative implementation.
  5. When recursive is True, use a recursive implementation. The recursive version should avoid one recursive call per bit so it remains practical for long inputs.

Do not use Python integer conversion on the complete binary strings, such as int(a, 2).

Constraints

  • 1 <= len(a), len(b) <= 100000
  • Each input contains only '0' and '1'
  • Inputs may contain leading zeros
  • The result must contain no leading zeros unless it is exactly '0'
  • Do not convert the complete strings to integers

Function Signature

def binary_xor(a, b, recursive=False):
Interviewer

Your question is Binary XOR Coding. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.