Product of Two Integers Without Multiplying
Updated
Problem statement
Compute a times b. The catch: the * operator, division and any library call that multiplies are off limits, so build the product with +, -, unary minus, <<, >>, & and comparisons. Either factor can be negative or zero, and the answer may need 64 bits, so return a long.
Function
multiply(a: int, b: int) → longExamples
Example 1
a = 13b = -6return = -7813 + 13 + 13 + 13 + 13 + 13 = 78. The signs of the two factors differ, which turns the result into -78.
Example 2
a = 0b = 2147483647return = 0Anything times zero is zero.
Example 3
a = -2147483648b = -1return = 2147483648The product is 2147483648, one more than the largest 32-bit integer, which is why the answer is a 64-bit integer.
Constraints
-2^31 <= a, b <= 2^31 - 1
More Capital One Onsite Interview questions
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
- Sort ColorsONSITE INTERVIEW · Seen Oct 2026
- Virtual Card Transaction ValidatorONSITE INTERVIEW · Seen Oct 2026
- Banking System with Top ActivityONSITE INTERVIEW · Seen Sep 2026
- Transaction Event StoreONSITE INTERVIEW · Seen Jul 2026
- Shortest Path in an Unweighted GraphONSITE INTERVIEW · Seen Jul 2026
- Duplicate Transactions Within 60 SecondsONSITE INTERVIEW · Seen May 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Cashback TrackerONSITE INTERVIEW · Seen Nov 2025
- Reverse a StringOA · ONSITE INTERVIEW · Seen Oct 2025
Similar questions at other companies
- Bayesian Scenario ClassifierAmazon · ONSITE INTERVIEW
- First Register Overflow TickNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
- Swap Two Integers Without a Temporary VariableOracle · ONSITE INTERVIEWOracle interview questions
- Add Two Binary StringsPeopleStrong · ONSITE INTERVIEWPeopleStrong interview questions
- Add BinaryMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Check Whether a Byte Is NonzeroZscaler · ONSITE INTERVIEWZscaler interview questions
- K-th Amount a Payout Machine Can GiveRippling · ONSITE INTERVIEWRippling interview questions
- Find the Lowest Set Bit PositionExpedia · OA · ONSITE INTERVIEWExpedia interview questions
- Knights, Knaves and Their StatementsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
Capital One interview experiences
- Capital One Coding OA Questions: 3 Experiences
- Capital One Experienced Engineer Power Day and OA: 6 Experiences
- Capital One New Grad OA Questions: 14 Experiences (Part 2 of 2)
- Capital One TDP Agentic Round (AI Coding): 7 Experiences
- Capital One SWE Power Day, Level Not Reported: 6 Experiences
- Capital One TIP Intern OA and Power Day: 13 Experiences