Problem · Math

Find the Wrong Digit Index in a Sum

EasyUpstartFULLTIMEOA

You are given three non-negative integers: num1, num2, and givenSum.

Compute the correct result of num1 + num2, then compare it with givenSum digit by digit in base 10, starting from the least significant digit (index 0).

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, treat the missing higher-order digits of the shorter number as 0 when comparing. The comparison continues through the maximum number of digit positions across both numbers.

Return the index of the first mismatching digit. Return -1 if every digit matches (i.e., givenSum equals num1 + num2).

Function Description

Complete the function findWrongDigitIndex in the editor below.

findWrongDigitIndex has the following parameters:

  • long num1: the first addend
  • long num2: the second addend
  • long givenSum: the sum to compare against

Returns

int: the index of the first mismatching digit (starting from 0 at the ones place), or -1 if all digits match.

Examples
01 · Example 1
num1 = 13
num2 = 7
givenSum = 19
return = 0

The correct sum is 20. The ones digit differs immediately: 0 versus 9.

02 · Example 2
num1 = 95
num2 = 5
givenSum = 100
return = -1

The provided sum is correct, so there is no mismatching digit.

Constraints

Constraints:

  • 0 <= num1, num2, givenSum <= 1018
  • The correct sum num1 + num2 may exceed 1018; use arbitrary-precision arithmetic when computing the correct sum.
  • Digits are compared in base 10 from least significant (index 0) to most significant position.
  • Missing higher-order digits are treated as 0 when the correct sum and givenSum have different numbers of digits.
  • Return -1 if and only if givenSum == num1 + num2.
More Upstart problems
drafts saved locally
public int findWrongDigitIndex(long num1, long num2, long givenSum) {
    // write your code here
}
num113
num27
givenSum19
expected0
checking account