Find the Wrong Digit Index in a Sum
Updated
Problem statement
You are given three non-negative integers: num1, num2, and givenSum.
Work out the correct sum num1 + num2. Then compare it with givenSum one digit at a time in base 10. Start from the ones digit (index 0) and move left.
The ones place is index 0, the tens place is index 1, and so on. If the correct sum and givenSum have different numbers of digits, the shorter one is missing some digits on the left. Treat each missing digit as 0. Keep comparing until you have checked every digit position of the longer number.
Return the index of the first digit that differs. Return -1 if every digit matches, which happens only when givenSum equals num1 + num2.
Complete the function findWrongDigitIndex.
findWrongDigitIndex gets these parameters:
long num1: the first number to addlong num2: the second number to addlong givenSum: the sum to check
It returns an int: the index of the first digit that differs (counting from 0 at the ones place), or -1 if all digits match.
Function
findWrongDigitIndex(num1: long, num2: long, givenSum: long) → intExamples
Example 1
num1 = 13num2 = 7givenSum = 19return = 0The correct sum is 20. The ones digits already differ: 0 versus 9. The ones place is index zero, so that is the answer.
Example 2
num1 = 95num2 = 5givenSum = 100return = -1The given sum is correct, so no digit differs and the answer is minus one.
Constraints
0 <= num1, num2, givenSum <= 1018- The correct sum
num1 + num2may be larger than1018. Use arbitrary-precision (big integer) arithmetic to compute it. - Digits are compared in base 10, from the ones place (index 0) up to the highest position.
- When the correct sum and
givenSumhave different numbers of digits, each missing digit on the left counts as0. - Return
-1if and only ifgivenSum == num1 + num2.
More Upstart Online Assessment questions
- Buffet Day GainsOA · Seen Sep 2026
- Case-Insensitive Anagram CheckOA · Seen Sep 2026
- Filter Duplicates While Preserving OrderOA · Seen Sep 2026
- Scale Ingredient QuantitiesOA · Seen Sep 2026
- ABO Parent Genotype CombinationsOA · Seen Aug 2026
- Chunked Byte Stream ChecksumsOA · Seen Aug 2026
- Cluster Size on DiskOA · Seen Aug 2026
- Double Substitution DecryptionOA · Seen Aug 2026
- Add Drama to Text GroupsOA · Seen Jul 2026
- Lexicographic Word FrequenciesOA · Seen Jul 2026
Similar questions at other companies
- Basic Item DiscountsPalantir · OAPalantir interview questions
- Diagonal Bounce TraversalAutodesk · OAAutodesk interview questions
- Costliest ChocolateRippling · OARippling interview questions
- Distinct Chapters Missed During a Cyclic AbsenceVisa · OAVisa interview questions
- Count Palindromic Base RepresentationsSEDEMAC · OA
- Reduce a Number to One DigitAccenture · OAAccenture interview questions
- Online No-Intercept Linear RegressionTwo Sigma · OATwo Sigma interview questions
- Calculate the Half SpreadMillennium · OAMillennium interview questions
- Plus Mult ArrayIBM · OAIBM interview questions
- Product Minus Sum of DigitsTikTok · OATikTok interview questions