Interview Question


Country: India




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

Just store these numbers in an array and add two numbers by performing addition on cells....like -

int[] add(int[] A, int[] B){
       int m = 0;
       if(A.size >=B.size){
           m = A.size;
       }else{
          m= B.size';
       }
       int[] result = new int[m+1];
       int index = m;
       int carry =0;
       (iterate both arrays from last to first element, till both array reach at first element){ 
           int x = A[i] +B[j] + carry;
           carry = x/10;
           int number = (x % 10);
           result[index] = number ;
       }
       return result;
}

- Ajeet January 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In place of taking int array it is better to take char array...............

- Amit Khatri January 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good job... neat and clean code...

- PKT January 18, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Amit Khatri
I just tried to provide the algorithm\concept. In actual implementation it will depend on the requirement. It can be in- strings, char arrays or int arrays.

In case of string or chars just add extra logic to parse integer from a char.

- Ajeet January 20, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Take the inputs as string and store them into integer arrays and perform addition something like below:
public class SumOfLargeNumber {
public static void main(String[] args) {
String a = "111111111111";
String b = "22222222222222";
int i = 0;
int firstInputLength = a.length();
int secondInputLength = b.length();
int sizeOfResult = firstInputLength > secondInputLength ? firstInputLength: secondInputLength;
int result[] = new int[sizeOfResult + 1];
int input1[] = new int[sizeOfResult];
int input2[] = new int[sizeOfResult];
while (i < firstInputLength) {
input1[sizeOfResult - i - 1] = Character.getNumericValue((a.charAt(firstInputLength - i - 1)));
i++;
}
i = 0;
while (i < secondInputLength) {
input2[sizeOfResult - i - 1] = Character.getNumericValue(b.charAt(secondInputLength - i - 1));
i++;
}
sumOfInputs(input1,input2,result);
}
private static void sumOfInputs(int[] input1, int[] input2, int[] result) {
int carry=0;
int sizeOfInputs=input1.length;
for(int i=sizeOfInputs;i>0;i--){
int sum = input1[i-1]+input2[i-1]+carry;
result[i]=sum%10;
if(sum/10>10)
carry=1;
else
carry=0;
}
result[0]=carry;
for(int i=0;i<=sizeOfInputs;i++)
System.out.print(result[i]);
}
}

- martin1990 January 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class CalculateLongIntegers {
	public static void main(String[] args){
		String s1 =  "123456278798238093532765432662476427646456353425635454854";
		String s2 = "1234562787982380935327654326624764276464563534248758758756";
		char[] c1 = s1.toCharArray();
		char[] c2 = s2.toCharArray();
		int[] c3;
		c3 = add(c1, c2);
		if(c3[0] != 0){
			System.out.print(c3[0]);
		}
		for(int i =1; i< c3.length; i++){
			System.out.print(c3[i]);
		}
	}

	private static int[] add(char[] c1, char[] c2) {
		int l1 = c1.length;
		int l2 = c2.length;
		int len = l1 > l2 ? l1: l2;
		int[] result = new int[len+1];
		int i = c1.length-1;
		int j = c2.length-1;
		int carry = 0;
		while(i >= 0 &&j >=0){
			result[len] = (c1[i] - '0' + c2[j] - '0' +carry) % 10;
			carry = (c1[i]-'0' + c2[j]-'0' + carry)/10;
			i--;
			j--;
			len--;
		}
		while(i>=0){
			result[len] = (c1[i] -'0' + carry) %10;
			carry = (c1[i] -'0'+ carry) /10;
			i--;
			len--;
		}
		while(j>=0){
			result[len] = (c1[j]-'0' + carry) %10;
			carry = (c1[j]-'0' + carry) /10;
			j--;
			len--;
		}
		return result;
	}
}

- ZhenyiLuo January 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int [] add(String a, String b){
		char [] chs_a = a.toCharArray();
		char [] chs_b = b.toCharArray();
		int m = chs_a.length>=chs_b.length? chs_a.length:chs_b.length;
		int [] result = new int [m+1];
		int carry = 0;
		int i = chs_a.length-1, j = chs_b.length-1;
		while (i>=0&&j>=0){
			result [m--] = ((chs_a[i]-'0') + (chs_b[j]-'0') + carry) %10;			
			carry = ((chs_a[i]-'0') + (chs_b[j]-'0') + carry)/10;			
			i--;
			j--;
		}
		
        while (i>=0){
        	result [m--] = (chs_a[i] -'0' + carry)%10;
        	carry = (chs_a[i] -'0'+ carry)/10;
        	i--;
        }
        
        
        while (j>=0){
        	result [m--] = (chs_b[j] - '0'+ carry)%10;
        	carry = (chs_b[j] -'0'+ carry)/10;
        	j--;
        }
		
        if (carry!=0){
        	result [m--] = carry;
        }
				
		return result;

}

- Scott January 19, 2014 | 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