Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Recursive Multiplication Without Operators

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

Your question is Recursive Multiplication Without Operators. 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

Thales TopSky-ATC contains performance-sensitive numerical routines where an arithmetic multiplication primitive may be unavailable or restricted. Implement multiplication of two positive integers using recursion while minimizing the number of arithmetic operations.

You must not use Python's *, /, or // operators. Bit shifts, addition, subtraction, comparisons, and bitwise operators are allowed.

Formal Specification

Implement recursive_multiply(a, b), where a and b are positive integers. Return the integer product a × b. The implementation must be recursive and should reduce the number of recursive calls by processing the smaller operand as the multiplier when useful.

A zero multiplier may occur in recursive subproblems, so the base case must return zero when the multiplier reaches zero.

Constraints

  • 1 <= a, b <= 10^9
  • Do not use *, /, or // operators.
  • The implementation must be recursive.
  • Inputs are positive integers.
  • The result must be returned as an exact integer.

Function Signature

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