## Expedia Interview Question for Software Engineer / Developers

Country: United States

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

``````public static  int[] addArray(int[] ar1, int ar2[])
{
int counter1=ar1.length-1;
int counter2=ar2.length-1;
int counter3= (counter1>counter2?counter1:counter2 )+1;
int[] ar3 = new int[counter3+1];
int carry=0;
while(counter3>-1)
{
if(counter1>=0) carry+=ar1[counter1--];
if(counter2>=0) carry+=ar2[counter2--];
ar3[counter3--]=carry%10;
carry /= 10;
}
return ar3;
}``````

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

Superb

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

short and concise ..too good soln

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

Super code. but need to handle leading 0. the result array is {0,6,9,1,2} instead.

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

Ronit.................

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

To avoid leading zeros, u can start storing result from start and den reverse the array

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

``````class AddingArrays
{
public static void main(String[] args)
{
int[] a = {1,2,3,4};
int[] b = {9,1,7,8};

int carry = 0;
int l = a.length;
int[] result = new int[l+1];
int p = l;
result=0;
for(int j=a.length-1; j>=0; j--)
{
int sum = a[j]+b[j]+carry;
int digit = sum%10;
carry = sum/10;
result[p--] = digit;

}

if(carry!=0)
result[p]=carry;

System.out.println();
for(int i=0;i<=l;i++)
System.out.print(result[i]+"\t");
System.out.println();
}
}``````

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

When they ask you to write code, do you have to write them on a piece of paper or you have a PC file editor?

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

Whiteboard.

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

I hate to say it but this solution works :) . At first I didn't like it because I didn't see what it did when the arrays were different sizes (and I still don't see how it takes care of this case but it works). Anyways, I did it my way (see below). I suggest others try it out. It's not as simple as the problem sounds (at least for me it wasn't). Nice job. Very compact; Good space complexity.

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

``````public int[] addArrays(int[] arr1, int[] arr2)
{
//make sure the arguments aren't null or empty
//return of null indicates the two array can't be added.
if(arr1 == null || arr1.length == 0)
return arr2;

if(arr2 == null || arr2.length == 0)
return arr1;

//see if the arrays are the same size
if(arr1.length != arr2.length)
{
//pad the array which has lesser elements with zero(s) to ensure the correct columns are added.
int[] temp;
if(arr1.length < arr2.length)
{
//arr1 is less

//make copy of arr1
temp = arr1;
//start index in new array
int start = arr2.length - arr1.length;
//pad with zeros; default value of int is zero.
arr1 = new int[arr2.length];

//copy original content
for (int j = start; j < arr1.length; j++)
arr1[j] = temp[j-start];

}
else
{
//arr2 is less

//make copy of arr2
temp = arr2;
//start index in new array
int start = arr1.length - arr2.length;
//pad with zeros; default value of int is zero.
arr2 = new int[arr1.length];

//copy original content
for (int j = start; j < arr2.length; j++)
arr2[j] = temp[j-start];
}
}
//arrays should be the same size at this point.
//create sum array and plus one in case there is a carry over
int[] sum = new int[arr1.length+1];
int carryOver = 0;
for (int i = arr1.length-1; i > -1; i--) {
//calculate sum on column. save it to in a variable so you wont have to keep repeating addition
int summation = carryOver + arr1[i] + arr2[i];
//store sum in array. ex. 18 mod 10 = 8 or 20 mod 10 = 0 or 25 mod 10 = 5.
sum[i+1]= summation % 10;
//calculate carry over. ex. 18 div 10 = 1 or 20 div 10 = 0 or 25 div 10 = 2.
carryOver = summation / 10;
}
//if we still have a carryover form the first indexes of arr1 and arr2 then put it in the sum array
if(carryOver > 0)
sum = carryOver;
else
//step isn't necessary but if you do not want to copy an array with an extra element that fill with zero only copy what you need.
sum = Arrays.copyOfRange(sum, 1, sum.length);
return sum;
}``````

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

``````static void Main(string[] args)
{
// Console.Write(findPrimes(179));
// printfa();

//ninekeys();

int[] int1 = new int{ 1, 2, 3, 4 };
int[] int2 = new int{ 5, 6 ,7, 8};
}

static int[] addTwo(int[] int1, int[] int2)
{

List<int> newList = new List<int>();

for (int i = int1.Length-1; i >=0; i--)
{
int newSingleValue = int1[i] + int2[i];

{
newSingleValue = newSingleValue + 1;
}

if (newSingleValue >= 10)
{
newSingleValue = newSingleValue - 10;
}

}

newList.Reverse();

return newList.ToArray<int>();

}``````

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

Hey can someone explain the Q and how the resultant array is calculated?

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

someone plz explain the question how after addition that array will be the result ?

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

1234+5678 = 6912

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

``````package com.dsna.puzzles;

//Given two arrays a1={1,2,3,4} and a2={5,6,7,8}, add the two array and return a new array n={6,9,1,2}

int[] a1 = new int[]{1,2,3,4};
int[] a2 = new int[]{5,6,7,8};

public static void main(String[] args) {
}

StringBuffer sbf1 = new StringBuffer();
for(int i=0; i<a1.length; i++){
sbf1.append(a1[i]);
}
int int1 = Integer.parseInt(sbf1.toString());

StringBuffer sbf2 = new StringBuffer();
for(int i=0; i<a2.length; i++){
sbf2.append(a2[i]);
}
int int2 = Integer.parseInt(sbf2.toString());

String result = int1 + int2 + "";

int[] c = new int[result.length()];

for(int i=0; i<result.length();i++){
c[i] = new Integer(result.charAt(i)+"");
}

for(int i : c){
System.out.print(i+ " ");
}

}

}``````

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

``````public static void main(String[] args) {

int[] array1 = {1, 2, 3, 4};
int[] array2 = {5, 6, 7, 8};
int[] result = new int[array1.length];

for (int i = array1.length - 1; i >= 0; i--) {
result[i] = array1[i] + array2[i];
}
int remainder = 0;
for (int j = result.length - 1; j >= 0; j--) {
int sum = result[j] + remainder;
remainder = sum / 10;
if (sum > 9) {
result[j] = (sum) % 10;
} else {
result[j] = sum + remainder;
}
}

for(int val : result){
System.out.println(val);
}

}``````

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

``````public string AddNumberInArray(string s1, string s2)
{
if (s1.Length < s2.Length) return AddNumberInArray(s2, s1);
char[] s1char = s1.ToCharArray();
char[] s2char = s2.ToCharArray();
int max = s1.Length;
int min = s2.Length;
int[] maxarray = new int[max];
int[] minarray = new int[min];
maxarray = Array.ConvertAll(s1char, c => (int)Char.GetNumericValue(c));
minarray = Array.ConvertAll(s2char, c => (int)Char.GetNumericValue(c));
int carry = 0;
int[] result = new int[max+1];
int minindexstop = 0;
int sum = 0;
for (int i = 1; i <= min; i++)
{
sum = minarray[minarray.Length - i] + maxarray[maxarray.Length - i] + carry;
result[result.Length - i] = sum%10;
carry = sum / 10;
minindexstop = i;
}
for(; minindexstop < result.Length-1; minindexstop++ )
{
sum = maxarray[maxarray.Length - minindexstop-1] + carry;
result[result.Length - minindexstop - 1] = sum%10;
carry = sum / 10;
}
result = carry;
string strresult = string.Join("", result);
if (strresult.StartsWith("0")) return strresult.Substring(1);
return strresult;
}``````

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

import java.util.Scanner;

public class ArraysSumOfTwoArrays {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);

int first = sn.nextInt();
int[] arr1 = new int[first];
for (int i = 0; i < arr1.length; i++) {
arr1[i] = sn.nextInt();
}

int second = sn.nextInt();
int[] arr2 = new int[second];
for (int i = 0; i < arr2.length; i++) {
arr2[i] = sn.nextInt();
}

for (int i = 0; i < Result.length; i++) {
if(Result[i]==0 && i==0) {

}else {
System.out.print(Result[i]+", ");
}
}
System.out.print("END");
}

public static int[] addArray(int[] arr1, int arr2[]) {
int firstlength = arr1.length - 1;
int secondlength = arr2.length - 1;
int counter;
if (firstlength > secondlength) {
counter = firstlength + 1;
} else {
counter = secondlength + 1;
}

int[] result = new int[counter + 1];
int carry = 0;
while (counter > -1) {
if (firstlength >= 0) {
carry = carry + arr1[firstlength--];
}
if (secondlength >= 0) {
carry = carry + arr2[secondlength--];
}
result[counter--] = carry % 10;
carry = carry / 10;
}
return result;
}
}

I hope It Helps You Guys

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.

### 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.