Amazon Interview Question
Software DevelopersCountry: India
Interview Type: Phone Interview
import java.util.ArrayList;
import java.util.Stack;
public class T1 {
private static int totalNum = 5;
private static int distance = 5;
private static int[] position = { 1, 3, 5, 9, 14 };
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> spreading = new ArrayList<>();
for (int i = 0; i < totalNum; i++) {
ArrayList<Integer> inside = new ArrayList<>();
for (int j = 0; j < totalNum; j++) {
if (i == j) {
continue;
} else {
if (position[i] < position[j])
if (position[i] >= (position[j] - distance)) {
inside.add(position[j]);
}
if (position[i] > position[j])
if (position[i] <= (position[j] + distance)) {
inside.add(position[j]);
}
}
} // end for j
spreading.add(inside);
} // end for i
// result
System.out.println(spreading);
// Analyze the result
int max = 0;
int min = totalNum;
Stack<Integer> minSpreading = new Stack<>();
Stack<Integer> maxSpreading = new Stack<>();
int positionCount = 0;
for (ArrayList<Integer> inside : spreading) {
positionCount++;
int count = 0;
for (Integer getInside : inside) {
count++;
}
if (count > max) {
max = count;
maxSpreading.add(positionCount);
}
if (count < min) {
min = count;
minSpreading.add(positionCount);
}
}
System.out.println("max : " + maxSpreading.peek());
System.out.println("min : " + minSpreading.peek());
}
}
In python:
def spread(N,d,position):
if len(position)<2:
return len(position),len(position)
distances = []
first = position[0]
distance = 1
for i,second in enumerate(position[1:]):
print("OH:",first,second,distances)
if abs(second-first)<=d:
distance+=1
else:
distances.append(distance)
distance = 1
if i==len(position[1:])-1:
distances.append(distance)
first = second
return min(distances),max(distances)
def spread(N,d,position):
if len(position)<2:
return len(position),len(position)
distances = []
first = position[0]
distance = 1
for i,second in enumerate(position[1:]):
print("OH:",first,second,distances)
if abs(second-first)<=d:
distance+=1
else:
distances.append(distance)
distance = 1
if i==len(position[1:])-1:
distances.append(distance)
first = second
return min(distances),max(distances)
package problems;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class VirusInfection
{
private static int totalNum = 5;
private static int distance = 5;
private static int[] arr = { 1, 3, 5, 9, 14 };
public static void main(String[] args)
{
Map map = new HashMap();
for (int i = 0; i < totalNum; i++)
{
int count = 0;
for (int j = 0; j < totalNum; j++)
{
if (i == j)
{
continue;
} else
{
if (6 > Math.abs(arr[i] - arr[j]))
{
count++;
map.put(arr[i], count);
}
}
} // end for j
} // end for i
int min = Integer.MAX_VALUE, min_pos = 0;
int max = 0, max_pos = 0;
Set set = map.entrySet();
Iterator itr = set.iterator();
while (itr.hasNext())
{
Entry entry = (Entry) itr.next();
int pos = (int) entry.getKey();
int count = (int) entry.getValue();
if (count < min)
{
min = count;
min_pos = pos;
}
else if (count > max)
{
max = count;
max_pos = pos;
}
}
System.out.println("best case when infected person is at position " + min_pos + " : will infect :" + min);
System.out.println("worst case when infected person is at position " + max_pos + " : will infect :" + max);
}
}
public class VirusSpreadInPark {
public static void main(String[] args) {
//
final int d = 5;
final int[] pos = {1,3,5,9,14};
//{1,3,5,9,14}
// spreads with d = 5
//{2,2,3,2,1}
final int n = pos.length;
int[] res = new int[n];
for(int i = 0; i < n; i++) {
int count = 0;
for(int j = 0; j < n; j++) {
if(i == j) {
continue;
}
if(Math.abs(pos[j] - pos[i]) <= d) {
count++;
}
}
res[i] = count;
}
int maxId = 0, minId = 0, max = res[0], min = res[0];
for(int x = 0; x < n; x++) {
if(res[x] > max) {
max = res[x];
maxId = x;
}
if(res[x] < min) {
min = res[x];
minId = x;
}
}
System.out.println("\nMax Position: " + pos[maxId]);
System.out.println("Min Position: " + pos[minId]);
}
}
public class VirusSpreadInPark {
public static void main(String[] args) {
//
final int d = 5;
final int[] pos = {1,3,5,9,14};
//{1,3,5,9,14}
// spreads with d = 5
//{2,2,3,2,1}
final int n = pos.length;
int[] res = new int[n];
for(int i = 0; i < n; i++) {
int count = 0;
for(int j = 0; j < n; j++) {
if(i == j) {
continue;
}
if(Math.abs(pos[j] - pos[i]) <= d) {
count++;
}
}
res[i] = count;
}
int maxId = 0, minId = 0, max = res[0], min = res[0];
for(int x = 0; x < n; x++) {
if(res[x] > max) {
max = res[x];
maxId = x;
}
if(res[x] < min) {
min = res[x];
minId = x;
}
}
System.out.println("\nMax Position: " + pos[maxId]);
System.out.println("Min Position: " + pos[minId]);
}
}
Here is my solution in JavaScript:
{{function CalcWorstBest(PD, n, d) {
var minC=null, maxC=null;
var suspectC;
var suspectList = [];
for (i=0; i<n; i++) {
let cntC = 0;
suspectC = i+1;
for (j=0; j<n; j++) {
if (i!=j) {
if (Math.abs(PD[i] - PD[j]) <= d) {
cntC++;
}
}
}
suspectList.push({suspectC, cntC});
if (minC === null || cntC <= minC) {
minC = cntC;
}
if (maxC === null || cntC >= maxC) {
maxC = cntC;
}
}
return {listBest: (suspectList.filter((el)=>el.cntC==minC)),
listWorst: (suspectList.filter((el)=>el.cntC==maxC))
};
}
console.log(CalcWorstBest([1,3,5,9,10], 5, 5));}}
function CalcWorstBest(PD, n, d) {
var minC=null, maxC=null;
var suspectC;
var suspectList = [];
for (i=0; i<n; i++) {
let cntC = 0;
suspectC = i+1;
for (j=0; j<n; j++) {
if (i!=j) {
if (Math.abs(PD[i] - PD[j]) <= d) {
cntC++;
}
}
}
suspectList.push({suspectC, cntC});
if (minC === null || cntC <= minC) {
minC = cntC;
}
if (maxC === null || cntC >= maxC) {
maxC = cntC;
}
}
return {listBest: (suspectList.filter((el)=>el.cntC==minC)),
listWorst: (suspectList.filter((el)=>el.cntC==maxC))
};
}
console.log(CalcWorstBest([1,3,5,9,10], 5, 5));
here is my solution in c++"
- jais October 16, 2020