Amazon Interview Question for Software Engineer in Tests


Country: United States
Interview Type: Phone Interview




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

public static void willItDie(int[] nums, int[] j, int n){
        //Find the index of n
        int nIndex = findIndex(nums,n);
        if (nIndex==-1) {
            System.out.println(true);return;
        }

        int jIterator = 0;
        while(jIterator<j.length){
            if(nIndex % j[jIterator] ==0){
                System.out.println("Yes, It will die at "+nIndex);return;
            }else{
                nIndex -= (nIndex / j[jIterator]);
            }
            jIterator++;
        }
        
        if(jIterator>=j.length){
            System.out.println("No It Won't Die!!");
        }else
            System.out.println("Yes, It will die at "+nIndex);
    }

    private static int findIndex(int[] nums, int n) {
        for (int i = 0; i < nums.length; i++) {
            if(nums[i]==n) return i+1;
        }
        return -1;
    }

If Jth element evenly divides the Nth element index then it died.
else the Nth element gets displaced by (CurrentIndex/Jth element)
Ex:
nums = 1,2,3,4,5,6,7,8,9,10
j = 2,3,4
n = 9

1st iter: j=2
1,3,5,7,9
9th index gets displaced by (9-9/2)
2nd iter: j=3
1,3,7,9
9th index gets displaced by (9-5/2)
3rd iter: j=4
9th index evenly divides 4 - So it died

- rohit December 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

{
naturalNumbersArray = []
Jarray = []

for i in range(1,101):
naturalNumbersArray.append(i)

ip = input().strip().split(" ")
for i in ip:
if i :
Jarray.append(int(i))

number = input()
number = int(number)
currIndex = number-1
for i in Jarray:
shiftIndex = (currIndex+1)%i * (currIndex - currIndex/i)
if shiftIndex == 0:
print("Will die while at index",currIndex+1)
exit(0)
else:
if shiftIndex <= currIndex:
currIndex = shiftIndex
print(number,"will survive")

}

If the element in Jarray evenly divides the current index value then it dies instantly else it gets shifted (currIndex - currIndex/i) times forward in the naturalNumbersArray i.e it shifts its position towards right and assigning new index value to it.

- Anonymous December 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

naturalNumbersArray = []
    Jarray = []

    for i in range(1,101):
        naturalNumbersArray.append(i)

    ip = input().strip().split(" ")
    for i in ip:
        if i :
            Jarray.append(int(i))

    number = input()
    number = int(number)
    currIndex = number-1
    for i in Jarray:
        shiftIndex = (currIndex+1)%i * (currIndex - currIndex/i)
        if shiftIndex == 0:
            print("Will die while at index",currIndex+1)
            exit(0)
        else:
            if shiftIndex <= currIndex:
                currIndex = shiftIndex
    print(number,"will survive")

If an element in Jarray divides currIndex evenly naturalNumbersArray element's index dies instantly else it shifts its position (currIndex-currIndex/i) times i.e shifting position towards right and if possible finally it survives, if no element in the Jarray is able to divide its position evenly.

- Mr.A.thewizard December 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

In ZoomBA:

// returns -1 if does not go bye bye, 
// else returns the index 
will_bye_bye( n , J ){
  // one liner in ZoomBA
  r = [1:n+1]
  for ( i = 0 ; i < size(J) ; i+=1 ){
    // select those whose index is divisible by the item index 
    r = select ( r ) :: {  J[i] /? ($.index + 1) } 
    if( empty(r) || r[-1] != n ){ return i }
  }
  return -1
}

- NoOne December 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {

int n;
vector <int> numbers(1e6);
vector <int> number_indces(1e6);

int divisor [] = {2,3,4};
// you can sort the divisor and eliminate the multiplier of small divisors
vector <int> div (divisor,divisor+3);

for(int i = 0 ; i < 1e6;i++){
numbers[i] = i;
number_indces[i] = i;
}

cin >> n;
bool die = false;
int divIdx = -1;
for(int i = 0 ; i < div.size();i++){
if(n%div[i] == 0){
die = true;
divIdx = i;
break;
}
}

if(!die){
cout <<"it will survive" <<endl;
}else{
for(int i = 0 ; i <= divIdx;i++){
int idx = 0;
for(int j = 0 ; j < numbers.size();j++){
if(i == divIdx && numbers[j] == n){
cout << "The number will die in index = "<<number_indces[j] << endl;
break;
}
if(number_indces[j] != -1){
if(numbers[j]%div[i] == 0){
number_indces[j] = -1;
}else{
number_indces[j] = idx++;
}
}
}
}

}


return 0;
}

- mo3az014 December 13, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Survival {
public static void main(String[] args) {
int a[] = {1,2,3,4,5,6,7,8,9,10};
int b[] = {2,3,4};
System.out.println(isSurvive(a,b,9));
}

public static int isSurvive(int a[], int b[], int n){

int pos = findPos(a,n);

int curpos = pos;

for (int i=0;i<b.length;i++){
if(curpos%b[i]==0){
System.out.println("It will die at pos: "+(curpos-1));
return curpos-1;
}
else{
int temp;
temp = curpos/b[i];
curpos = curpos - temp;
}
}

System.out.println("It Survived");
return -1;

}

public static int findPos(int a[],int n){

HashMap<Integer,Integer> hm = new HashMap<Integer,Integer>();
for(int i=0;i<a.length;i++){
hm.put(a[i],i);
}
return hm.get(n) + 1;
}


}

- rxk56100 December 25, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void Main(string[] args)
        {
            var n = 6;
            var J = new[]{2, 3, 4};

            var a = Enumerable.Range(1, 10).ToList();

            bool willDie = false;

            var nIndex = a.IndexOf(n) + 1;
            if (nIndex == 0)
            {
                Console.WriteLine(true);
                return;
            }

            foreach (var jump in J)
            {
                if (nIndex % jump == 0)
                {
                    willDie = true;
                    break;
                }

                nIndex -= nIndex/jump;
            }

            Console.WriteLine(willDie);
            
        }

- Anonymous December 30, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The question said to use streams, yes?

- Pinecone January 03, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

First we need find the index of the natural number in the stream. In the given example, the index of n is n-1.

/**
	 * 
	 * @param initialIndex: the index of some element in the original series of natural numbers.
	 * @param operations: int array to hold the operation.
	 * @param indexOfOperation: index of the target operation.
	 * @param indexOfCurrentOperation: the current operation in recursive computation. 
	 * 								   The initial value is 0; final value is indexOfOperation; 
	 * 								   this value increases by 1 in each recursion. 
	 * @return
	 */
	public boolean isAlive(int initialIndex, int[] operations, int indexOfTargetOperation, int indexOfCurrentOperation){
		
		if(indexOfCurrentOperation > indexOfTargetOperation) return true;
		
		int operation = operations[indexOfCurrentOperation];
		
		if((initialIndex+1)%operation == 0){
			System.out.println("die in operation: "+operation);
			return false;
		}
		
		//index of the surviving element in new array after the operation
		initialIndex = initialIndex-(initialIndex+1)/operation; 
		indexOfCurrentOperation++;
		
		return isAlive(initialIndex, operations, indexOfTargetOperation, indexOfCurrentOperation);
		
	}

- yankeson2013 February 08, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

def Remove():
i = 0
data = []
while i < 1111:
data.append(i+1)
i = i + 1
j = [2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40]
for each in j:
if each - 1 > len(data):
print ("out of range at: ", each)
index = each - 1
while index < len(data):
data.pop(index)
index = index + each - 1
print data

- Anonymous May 10, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int isSurvied(int[] arr, int[] j)
{
if (arr == null || arr.Length == 0)
return -1;
if (j == null || j.Length == 0)
return arr.Length - 1;

int count = arr.Length;
for(int i = 0; i < j.Length; i++)
{
count = count/j[i] + count %j[i];
}
if (count > 0)
return count - 1;
else
return -1;
}

- Upendra P July 08, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int isSurvied(int[] arr, int[] j)
        {
			if (arr == null || arr.Length == 0)
				return -1;
			if (j == null || j.Length == 0)
				return arr.Length - 1;

			int count = arr.Length;
			for(int i = 0; i < j.Length; i++)
            {
				count = count/j[i] + count %j[i];
			}
			if (count > 0)
				return count - 1;
			else
				return -1;

}

- Upendra P July 08, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int isSurvied(int[] arr, int[] j)
        {
			if (arr == null || arr.Length == 0)
				return -1;
			if (j == null || j.Length == 0)
				return arr.Length - 1;

			int count = arr.Length;
			for(int i = 0; i < j.Length; i++)
            {
				count = count/j[i] + count %j[i];
			}
			if (count > 0)
				return count - 1;
			else
				return -1;
        }

- Upendra P July 08, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public int isSurvied(int[] arr, int[] j)
{
if (arr == null || arr.Length == 0)
return -1;
if (j == null || j.Length == 0)
return arr.Length - 1;

int count = arr.Length;
for(int i = 0; i < j.Length; i++)
{
count = count/j[i] + count %j[i];
}
if (count > 0)
return count - 1;
else
return -1;
}

- Upendra P July 08, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int isSurvied(int[] arr, int[] j)
        {
			if (arr == null || arr.Length == 0)
				return -1;
			if (j == null || j.Length == 0)
				return arr.Length - 1;

			int count = arr.Length;
			for(int i = 0; i < j.Length; i++)
            {
				count = count/j[i] + count %j[i];
			}
			if (count > 0)
				return count - 1;
			else
				return -1;
        }

- Upendra P July 08, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static int isSurvied(int[] arr, int[] j)
{
if (arr == null || arr.Length == 0)
return -1;
if (j == null || j.Length == 0)
return arr.Length - 1;

int count = arr.Length;
for(int i = 0; i < j.Length; i++)
{
count = count/j[i] + count %j[i];
}
if (count > 0)
return count - 1;
else
return -1;
}

- Upendra P July 08, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

The question is not clear. What number the j array starts from ? always 2 or anything else. Like if it has 1 on some index then what?. If j has numbers 2 or greater than two then I have very simple answer.

public int countDie(int[] arr,int[] j,int[] result)
	{
		int count = arr.length;
		for(int i=0;i<j.length;i++)
		{
			count = count/j[i];
			result[0] = count;
			result[1] = i;
			result[2] = i-1;
			
			if(count == 0)
			{
				return i;
			}
			
		}
		return 0;
	}

where result[0] is the number of elements survive, the result[1] has the position where it will die and the result[2] has the position where last it survived. The return is the dieng position while return 0 means will survive.

- Sahib December 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

The question is not clear. What are the possible operations in j? if it contained 1 then which element to remove. ? What is the function of n?. What I understood is if j has 2 or greater numbers then this is very simple.

public int countDie(int[] arr,int[] j,int[] result)
	{
		int count = arr.length;
		for(int i=0;i<j.length;i++)
		{
			count = count/j[i];
			result[0] = count;
			result[1] = i;
			result[2] = i-1;
			
			if(count == 0)
			{
				return i;
			}
			
		}
		return 0;
	}

where return 0 means survived, return i means died at ith position, result[0] means remaining elements count. result[1] meains position at which died, result[i] means position at which last survived

- Sahib.zer December 09, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

hmh... looks like math problem
I guess...
J0 leaves out all evens : n%2==0 die
J1 leaves out every thirds from the left : (n - n/2)%3==0 die
J2 leaves out every fourths from the left : (n-n/2 - (n-n/2)/3)%4==0 die
else survive

- ghcho20 December 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

hmh... looks like math problem
My another attempt...
J0 leaves out all evens : n%2==0 die
J1 leaves out every thirds from the left : (n - n/2)%3==0 die
J2 leaves out every fourths from the left : (n-n/2 - (n-n/2)/3)%4==0 die
else survive

- ghcho20 December 21, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

In Python:
nums is last natural num range till you want feed
n is element in sequence that want not included

a1 = [ i for i in range(1, nums+1)]
a2 = [ i for i in a1 if I%n !=0]

- Nitin K January 22, 2017 | 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