Amazon Interview Question for Backend Developers


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

Hint: use this method as a way of summing big numbers:

public String bigSum(String s1, String s2) {
    int lengthS1 = s1.length();
    int lengthS2 = s2.length();

    int diff = Math.abs(lengthS1 - lengthS2);

    String prefix = repeatString("0", diff);


    if (lengthS1 < lengthS2) {
        s1 = prefix + s1;
    } else if (lengthS2 < lengthS1) {
        s2 = prefix + s2;
    }

    StringBuilder out = new StringBuilder();
    int buf = 0;
    for (int i = s1.length() - 1; i >= 0; i--) {
        int n1 = Integer.parseInt(String.valueOf(s1.charAt(i)));
        int n2 = Integer.parseInt(String.valueOf(s2.charAt(i)));

        int sum = buf + n1 + n2;
        buf = 0;
        if (sum >= 10) {
            sum = sum % 10;
            buf++;
        } else {
            buf = 0;
        }

        if (buf > 0 && i == 0) {
            out.insert(0, "1").insert(1, sum);
        } else {
            out.insert(0, sum);
        }
    }


    return out.toString();
}

- denis.zayats March 06, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

this would be my answer, with a few holes of course...

private static String addNumbers(String[] no)   // assumes all given strings are in decimal no format
{
    String sum = "";
    int bal = 0;
    int max = 0;
    for (String no1 : no)   // max length of string nos
        if (max < no1.length())
            max = no1.length();
    for (int count = 1; count <= max || bal > 0; count++)
    {
        for (String no1 : no)                                   // \    find sum of individual digits
            if (no1.length() - count >= 0)                      // -->  assumes that the  sum of individual
                bal += (no1.charAt(no1.length() - count)-'0');  // /    digits (bal) never exceeds int max limit
        sum=""+(bal%10)+sum;
        bal/=10;
    }
    return sum;
}

- PeyarTheriyaa March 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is terribly language specific question. In fact I would go to the point saying it is a non question for anything above C. In C++ we already have arbitrary precision integers.
[ stackoverflow.com/questions/2568446/the-best-cross-platform-portable-arbitrary-precision-math-library ]

In a slightly better human/declarative language -- this is as cakewalk as :

sum ( file( 'input.txt' ) ) as { int(  $.o ) } // this is ZoomBA

sum(int(v) for v in open('input.txt') ) # Python

Now, same in Java is actually left as exercise.

- NoOne March 07, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

As @NoOne pointed out, the problem is extremely trivial if we take the assumption that we use the int() function. However, if we are not allowed to do so, then we have to add strings and use subtraction to get the integer value for each character in the string (this is similar to the atoi function approach). I have outlined both solutions below. I am making an assumption that we can still use string functions (since that's our input) and I use one another iterable function.

Simple solution (using int)

def addNumbersUsingSumFunction(numericStrings):
    return str(sum(int(numString) for numString in numericStrings))

Adding Strings Solution (without using int())

import itertools
def addNumbers(numericStrings):
    reversedNums = [list(number)[::-1] for number in numericStrings]
    result = ''
    carry = 0
    for ind, digits in enumerate(itertools.zip_longest(*reversedNums, fillvalue='0')):
        innerSum = carry
        for digit in digits:
            innerSum += ord(digit) - ord('0')
        carry = innerSum // 10
        result += str(innerSum % 10)
    if carry:
        result += str(carry)
    return result[::-1]

Test Code:

print(addNumbersUsingSumFunction(['199', '1', '1'])) # 201
print(addNumbersUsingSumFunction(['999', '99', '9'])) #1107
print(addNumbersUsingSumFunction(['333', '333', '334'])) #1000

print(addNumbers(['199', '1', '1'])) # 201
print(addNumbers(['999', '99', '9'])) #1107
print(addNumbers(['333', '333', '334'])) #1000

- prudent_programmer March 09, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

string add(){
    int mx = 0,c = 0;
    string s[3]={"23456","21289124","1490280812"},t;
    for(int i=0;i<3;i++)
        if(mx < s[i].size())
            mx = s[i].size();
    for(int i=0;i<3;i++)
        while(s[i].size()<mx)
            s[i]="0"+s[i];
    for(int i=mx-1;i>=0;i--){
        int k = 0;
        for(int j=0;j<3;j++)
            k+=(s[j][i]-'0');
        k += c;
        t = to_string(k%10) + t;
        c = k/10;
    }
    return t;

- bgm March 11, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int mx = 0,c = 0;
    string s[3]={"23456","21289124","1490280812"},t;
    for(int i=0;i<3;i++)
        if(mx < s[i].size())
            mx = s[i].size();
    for(int i=0;i<3;i++)
        while(s[i].size()<mx)
            s[i]="0"+s[i];
    for(int i=mx-1;i>=0;i--){
        int k = 0;
        for(int j=0;j<3;j++)
            k+=(s[j][i]-'0');
        k += c;
        t = to_string(k%10) + t;
        c = k/10;
    }
    cout<<t;

- ramtajogi March 11, 2018 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More