## Amazon Interview Question for SDE1s

• -2

Team: WebStore
Country: India
Interview Type: Phone Interview

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

``````#include <iostream>
#include <algorithm>

using namespace std;
int multiply(int A[], int lenA, int B[], int lenB) {

int len = max(lenA, lenB);

int sum = 0;

for (int i=0; i<len; i++) {
sum += A[i%lenA]*B[i%lenB];
}

return sum;

}

int main(void) {

int A[] = {1, 2, 3, 4, 5};
int B[] = {2, 1};

int lenA = sizeof(A)/sizeof(int);
int lenB = sizeof(B)/sizeof(int);

cout << multiply(A, lenA, B, lenB) << endl;

return 0;
}``````

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

public static int sumProduct(int[] a, int[] b) {
if(a == null || b == null)
return 0;
int length = a.length > b.length ? a.length : b.length;
int sumProduct = 0;
int i = 0;

while(i < length) {
sumProduct += a[ i % a.length ] * b[ i % b.length ];
i++ ;
}
return sumProduct;
}

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

``````public static int sumProduct(int[] a, int[] b) {
if(a == null || b == null)
return 0;
int length = a.length > b.length ? a.length : b.length;
int sumProduct = 0;
int i = 0;

while(i < length) {
sumProduct += a[ i % a.length ] * b[ i % b.length ];
i++ ;
}
return sumProduct;
}``````

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

``````int multiply(int A[], int B[])
{
int lenA = sizeof(A)/sizeof(int);
int lenB = sizeof(B)/sizeof(int);
int sum = 0;
int i = 0, j = 0;
if(lenA > lenB)
{
for(; i < lenA; i++)
{
sum += A[i % lenA] * B[i % lenB];
}
}
else if(lenB > lenA)
{
for(; i < lenB; i++)
{
sum += B[i % lenB] * A[i % lenA];
}
}
else
{
for(; i < lenA; i++)
{
sum += A[i] * B[i];
}
}
return sum;
}``````

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

Above answer is posted by me.

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

The way I see it, it should also work without the else part ( using any of the above two conditions.. i.e. lenA>=lenB or lenA<=lenB )..

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

result = 0 ,j =0;
for(i=0; i<n ;i++)
{
if(sizeof(b)/b[0] == j && i < sizeof(a)/a[0])
j =0;

if(sizeof(b)/b[0] == i )
{
c[i] = a[i] * b[j];
j++;
}
else
{
c[i] = a[i] * b[i];
}
result = result + c[i];
}

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

Hey Nitin can you tell me more about the interview procedure , what all question they asked?

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

``````#include <stdio.h>

int main(int argc, char *argv[])
{
int a[5]={1,2,3,4,5},b[2]={2,1};
int k=0,i=0,j=0;
while(i<5)
{
k=k+(a[i]*b[j]);
i++;
j=i%2;

}
printf("%d",k);

return 0;
}``````

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

package algo;

public class ArrayMul {

public static double arrayMultiplication(int []arr1, int []arr2){
int len1= arr1.length;
int len2=arr2.length;
System.out.println(""+len1+""+len2);
double sum =0.0;
if(len1>len2){
for(int i=0,j=0;i<len1;i++){
sum = sum + arr1[i]*arr2[j];
j++;
if(j==len2){
j=0;
}
}
}else{
for(int i=0,j=0;i<len2;i++){
sum = sum + arr1[j]*arr2[i];
j++;
if(j==len1){
j=0;
}
}

}
return sum;
}

public static void main(String args[]){
int [] a= {2,1};
int [] b={1,2,3,4,5};
System.out.println("Sum :"+ArrayMul.arrayMultiplication(a, b));
}
}

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

``````package algo;

public class ArrayMul {

public static double arrayMultiplication(int []arr1, int []arr2){
int len1= arr1.length;
int len2=arr2.length;
System.out.println(""+len1+""+len2);
double sum =0.0;
if(len1>len2){
for(int i=0,j=0;i<len1;i++){
sum = sum + arr1[i]*arr2[j];
j++;
if(j==len2){
j=0;
}
}
}else{
for(int i=0,j=0;i<len2;i++){
sum = sum + arr1[j]*arr2[i];
j++;
if(j==len1){
j=0;
}
}

}
return sum;
}

public static void main(String args[]){
int [] a= {2,1};
int [] b={1,2,3,4,5};
System.out.println("Sum :"+ArrayMul.arrayMultiplication(a, b));
}
}``````

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

``````class Product {
Product(int sum, int[] array) {
this.sum = sum;
this.array = array;
}

final int sum;
final int[] array;
}

Product arrayProduct(int[] a, int[] b) {
int sum = 0;
int[] array = new int[Math.max(a.length, b.length)];
for (int i = 0; i < array.length; i ++) {
array[i] = a[i % a.length] * b[i % b.length];
sum += array[i];
}
return new Product(sum, array);
}``````

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

``````public static int multiplyAndSumArrays(int[] arrayA, int[] arrayB) {
int result = 0;

if (arrayA.length > arrayB.length) {
int arrayBIndex = 0;
for (int i = 0; i < arrayA.length; i++) {
arrayBIndex = i % arrayB.length;
result += arrayA[i] * arrayB[arrayBIndex];
}
}
else {
result = multiplyAndSumArrays(arrayB, arrayA);
}

return result;
}``````

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

Correction to the if statement.

``if (arrayA.length >= arrayB.length)``

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

``````public int multiplyAndSumArrays()
{
List<int> lsA = new List<int>(new int[] { 1, 2, 3, 4, 5 });
List<int> lsB = new List<int>(new int[] { 2, 1 });
int k = 0;

foreach (int i in lsA)
{
k = k + i * lsB[(lsA.IndexOf(i) % lsB.Count)];
}

return k;
}``````

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

``````int[] arr1 = {1,2,3,4,5};
int[] arr2 = {2,1};

int sum = 0;

for(int i = 0,j=0; i<arr1.length; i++,j++){

sum = sum + arr1[i]*arr2[j];
if(j == arr2.length-1)
j = -1;
}

System.out.println("Sum -- " + sum);``````

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

public static int arraySum(int[] a, int[] b) {
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i] * b[i%b.length];
}
return sum;
}

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

``````public static int getSum(int[] a, int[] b) {
int sum = 0, len1 = a.length, len2=b.length;
for(int i=0; i<len1;i++) {
sum += a[i]*b[i%len2];
}

return sum;
}
public static int prepare(int[] a, int[] b){
if (a.length > b.length)
return getSum(a,b);
else
return getSum(b,a);
}``````

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

``````class ProdArray {
private static int prodOfArray( int[] A, int[] B) {
int i, j, count=0;
if (A.length < B.length) {
int sum = prodOfArray(B, A);
return sum;
}
else {
for (i=0; i<A.length; i++) {
j = i % B.length;
count += A[i] * B[j];
System.out.println(A[i] + " * " + B[j] + "\t= " + A[i] * B[j]);
}
return count;
}
}

public static void main(String[] args) {
int[] a = {2, 7, 8, 9};
int[] b = {2, 13, 10};

System.out.println(prodOfArray(a,b));
}``````

}

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.