## Amazon Interview Question for Software Engineer / Developers

• 0

Country: United States
Interview Type: Written Test

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

Edit: I have been asked for the algorithm. I will do so below, but before I give the algorithm, note that this is an interview question. Any attempt to write pages of code is inherently wrong. There is always a trick to these things, and the trick here is recursion.

Algorithm:

(*) Let y be your second number (y should loop from 0 to 9)
(*) Now create a new number, call it test (for want of a better name). test is x+y, but there is a condition: test must be greater than 0 and less than 9. If this condition is not passed, then the number is not even considered.
(*) Now that we have our candidate number, x,y,test, we need to recurse to find all numbers that satisfy the property we are looking for, but with x,y,test as the base number.

The problem basically involves both iteration and recursion. Definitely not the most efficient solution (a greedy solution would be more efficient), but guaranteed to be the solution that the interviewer is looking for.

This can easily be solved with recursion. Here is a recursive solution:

``````void an(int start, int end, int num) {
if (num > end) return;
if (num > 0 && num > start) cout << num << " ";

for (int i=1; i < 9; i++)
for (int j=0; j < 9; j++) {
int test = i+j;
if (test <= 9)
an(start, end, num*1000 + i*100 + j*10 + test);
}
}

int main() {
int startRange = 800000, endRange = 900000;
an(startRange, endRange, 0);
return 0;
}``````

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

It is probably only correct when an called first time - from main?

What is wring with

123011 ?
123000 ?

Am I wrong?

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

Hi

It all depends on how you interprate zero. So in an interview, you should probably ask. For example, if 123000 is taken as correct, then what about 123000000 or 123000000000 etc etc. Where do you stop. I do agree with you though about 011. It is a trivial change to make this adjustment. I did it with one extra line of code. Make contact if you want my solution.

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

Doesn't works for the numbers like 123446(12+34=46), etc.
But still nice solution for other cases.

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

it does check for the third scenario mentioned in the question: 122436 (12+24=36) . plz correct if my understanding is not correct

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

Do the two numbers that we add have to be of same length? for example :- Is 1241125 an additive number? (for me it is since 124 +1 =125). But I want to clear it

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

``````public class AddativeNumberSequence {

long[] digitsinnumber = toArray(number);
long numberofdigits = digitsinnumber.length;
boolean result = true;
boolean finalflag = false;
for (int i = 0; i < numberofdigits / 3; i++) {
for (int j = 0; j <= numberofdigits - 3 * (i + 1); j = j + i + 1) {
long part1 = makeNumberFromArray(digitsinnumber, j, j + i,
i + 1);
long part2 = makeNumberFromArray(digitsinnumber, j + i + 1, j
+ 2 * i + 1, i + 1);
long part3 = makeNumberFromArray(digitsinnumber, j + 2
* (i + 1), j + 2 * (i + 1) + i, i + 1);
if (part1 + part2 != part3 && !finalflag) {
result = false;
break;
} else {
result = true;
}
}
if (result) {
finalflag = true;
} else {
finalflag = false;
}
}
return finalflag;
}

public void findAddativeNumberIn(long start, long end) {
long tempstart = start;
while (tempstart <= end) {
System.out.print(tempstart + " ,");
}
tempstart++;
}
}

public long[] toArray(long inputnumber) {
long[] numberarray;
int numlengthcount = 0;
long tempnumber = inputnumber;
while (tempnumber > 0) {
numlengthcount++;
tempnumber = tempnumber / 10;
}
numberarray = new long[numlengthcount];
tempnumber = inputnumber;
int i = 0;
while (i < numlengthcount) {
numberarray[numlengthcount - 1 - i] = tempnumber % 10;
tempnumber = tempnumber / 10;
i++;
}
return numberarray;
}

public long makeNumberFromArray(long[] numarray, int start, int end,
int digitcount) {
long sum = numarray[end];
int prod = 1;
int i = 1;
while (i < digitcount) {
prod *= 10;
sum += numarray[end - i] * prod;
i++;
}
return sum;
}

}``````

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

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

``````public class AdditiveNumberProperty {

private static List<Integer> additiveNumbList_ = new ArrayList<Integer>(Arrays.asList(123459,314538,122436,123456579));

public static void main(String[] args) {

StringBuilder builder = new StringBuilder();

}
}
}

private static boolean isAdditiveNumber(String number,StringBuilder builder) {

if (number.length() % 3 == 0) {

int sizeOfAdditiveNumber = number.length() / 3;
int numb1=-1;
int numb2=-1;
int numb3=-1;
int counter=0;

for (int i=0;i<number.length();i++) {
counter++;
}
if (counter==1) {
}
else if (counter==2) {
}
else if (counter==3) {
}
}

if((numb1+numb2)==numb3) {
//ie. 123459 (1+2=3, 4+5=9)
builder.append(" "+numb1+"+"+numb2+"="+numb3);
return true;
}

if (number.length() % 2 == 0) {
int middle=number.length()/2;
}
}

}

}``````

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

Working R code - simple approach.
1. Cycle through an index of i
2. Cycle through the number while iterating by i to check that every 2 segments of length i add up to the next segment of length i
3. If it doesn't in one instance, cancel the cycle and pick the next index
4. If it works for all segments in your number then print that number

On a side note, you guys make some very complicated programs.

``````for(k in 210000:220000) {
for (i in 2:floor(nchar(k)/2)-1){

if (nchar(k)%%i==0) {
if (2+i <= (nchar(k)-i+1)) {
for (h in seq(1+2*i,((nchar(k)-i+1)),i)) {

if (as.numeric(substr(k,h,h+i-1)) != as.numeric(substr(k,h-2*i,h-i-1))+as.numeric(substr(k,h-i,h-1)))
{
break
}
else
{
if (h == nchar(k)-i+1)
{
print(k)
break
}

}

}
}
}
}
}``````

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

``````#include <iostream>
#include <cstdlib>

using namespace std;

long
pow10(unsigned int n)
{
long num = 1;
while (n-- > 0) {
num *= 10;
}

return num;
}

unsigned int
log10(long num)
{
unsigned int n = 0;

while (num >= 10) {
n++;
num /= 10;
}

return n;
}

long
AdditiveNumbersHelper(long left_num, int left_digits, int right_digits, long start, long end)
{
if (left_num > end) {
return 0;
}

long count = 0;
if ((left_num > 0) && (left_num >= start)) {
cout << left_num << endl;
count++;
}

if (right_digits < 3) {
return count;
}

long left_num_shifted = left_num * pow10(left_digits);

for (int w = 1; w <= (right_digits / 3); w++) {
long p10w = pow10(w);
int curr_left_digits = left_digits + 3*w;
int curr_right_digits = right_digits - 3*w;
long w_start = (left_digits == 0) ? (p10w / 10) : 0;
long w_end = p10w - 1;

for (long part1 = w_start; part1 <= w_end; part1++) {
for (long part2 = w_start; part2 <= w_end - part1; part2++) {
long part3 = part1 + part2;
long curr_left_num = left_num_shifted + (((part1 * p10w) + part2) * p10w) + part3;

count += AdditiveNumbersHelper(curr_left_num, curr_left_digits, curr_right_digits, start, end);
}
}
}

return count;
}

long
{
int digits = log10(end) + 1;

long count = AdditiveNumbersHelper(0, 0, digits, start, end);

return count;
}``````

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

static String start = "100";
static String end = "400000";
public static void main(String[] args) {

}
private static void printAdditive(String res, String data, int n) {

if((res.length()%2==0 || n==1) && res.length()>1 && n>0 && n!=2) {
int sum = Integer.parseInt(res.charAt(res.length()-1)+"") + Integer.parseInt(res.charAt(res.length()-2)+"");
if(sum<10) {
res+=Integer.toString(sum);
n-=1;
}
else return;
}
if(n==0 || n==end.length()-start.length()) {
if(Integer.parseInt(res)>Integer.parseInt(start) && Integer.parseInt(res)<Integer.parseInt(end))
System.out.println(res);
if(n==0) return;
}
for(int i =0;i<data.length();i++) {
}
}

}

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

For Two number Range : like x and y

public class Sample2 {

public static void main(String args[])
{
for(int i=0;i<=9;i++)
{
for(int j=0;j<=9;j++)

{
for(int k=0;k<=9;k++)

{
if(i+j==k)
{
System.out.println(i+""+j+"="+k);
}
}
}
}
}

}

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

Do the two numbers that we add have to be of same length? for example :- Is 1241125 an additive number? (for me it is since 124 +1 =125). But I want to clear it

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

private int numberOfDigit(int number) {
int value = 1;
int tmp = number;
while (tmp >= 10) {
++value;
tmp /= 10;
}
return value;
}

private int extractNumber(int[] array, int offset, int length) {
if (length == 0) {
return -1;
}

int value = 0;
for (int i = offset; i < offset + length; i++) {
value = (value * 10 + array[i]);
}
return value;
}

private int matchNumber(int[] array, int offset, int number) {
int size = numberOfDigit(number);
for (int i = offset + size - 1; i >= offset; i--) {
int num = number % 10;
number /= 10;
if (array[i] != num) {
return -1;
}
}
return size;
}

private boolean additive(int[] array, int offset, int length, StringBuilder builder) {

if (offset >= array.length) {
return true;
}

// one number + one number == at least one number
if (offset + (3 * length) > array.length) {
return false;
}

int num1 = extractNumber(array, offset, length);
int num2 = extractNumber(array, offset + length, length);
int sum = num1 + num2;
int sumSize = matchNumber(array, offset + (2 * length), sum);

if (sumSize <= 0) {
return false;
}

if (builder.length() > 0) {
builder.append(", ");
}

builder.append(num1).append("+").append(num2).append("=").append(sum);
return additive(array, offset + 2 * length + sumSize, length, builder);
}

String run(int[] array) {
int length = 1;
StringBuilder builder;
boolean result;
do {
builder = new StringBuilder();
result = additive(array, 0, length, builder);
++length;
} while (!result && length * 3 <= array.length);

if (result) {
builder.insert(0, "(");
builder.append(")");
return builder.toString();
} else {
return null;
}
}
}

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

``````public class AdditiveNumbers {

private int numberOfDigit(int number) {
int value = 1;
int tmp = number;
while (tmp >= 10) {
++value;
tmp /= 10;
}
return value;
}

private int extractNumber(int[] array, int offset, int length) {
if (length == 0) {
return -1;
}

int value = 0;
for (int i = offset; i < offset + length; i++) {
value = (value * 10 + array[i]);
}
return value;
}

private int matchNumber(int[] array, int offset, int number) {
int size = numberOfDigit(number);
for (int i = offset + size - 1; i >= offset; i--) {
int num = number % 10;
number /= 10;
if (array[i] != num) {
return -1;
}
}
return size;
}

private boolean additive(int[] array, int offset, int length, StringBuilder builder) {

if (offset >= array.length) {
return true;
}

// one number + one number == at least one number
if (offset + (3 * length) > array.length) {
return false;
}

int num1 = extractNumber(array, offset, length);
int num2 = extractNumber(array, offset + length, length);
int sum = num1 + num2;
int sumSize = matchNumber(array, offset + (2 * length), sum);

if (sumSize <= 0) {
return false;
}

if (builder.length() > 0) {
builder.append(", ");
}

builder.append(num1).append("+").append(num2).append("=").append(sum);
return additive(array, offset + 2 * length + sumSize, length, builder);
}

String run(int[] array) {
int length = 1;
StringBuilder builder;
boolean result;
do {
builder = new StringBuilder();
result = additive(array, 0, length, builder);
++length;
} while (!result && length * 3 <= array.length);

if (result) {
builder.insert(0, "(");
builder.append(")");
return builder.toString();
} else {
return null;
}
}
}``````

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.