## Amazon Interview Question for Software Engineer / Developers

• 1
of 1 vote

Team: Amazon Video
Country: UK
Interview Type: In-Person

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

One solution can be using the PriorityQueue

``````import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class PrioRQueueExample {

public static void main(String[] args){

PriorityQueue<Double> pq = new PriorityQueue<Double>((x,y)-> {Double z = y-x;return z.intValue(); });
PrioRQueueExample pqe = new PrioRQueueExample();

//Number of ATMs to return i.e. K
int num_ATMs = 3;

double curr_loc = 0.00;

Map<String,Double> nallATMLocs = new HashMap<String,Double>();

//Map of ATM names and their distance co-ordinates
nallATMLocs.put("atm1",45.0);
nallATMLocs.put("atm2",78.0);
nallATMLocs.put("atm3",54.0);
nallATMLocs.put("atm4",64.0);
nallATMLocs.put("atm5",35.0);
nallATMLocs.put("atm6",42.0);
nallATMLocs.put("atm7",57.0);
nallATMLocs.put("atm7",1.00);

nallATMLocs.forEach((atm,dist) ->{if(pq.size() < num_ATMs){
else{
if(	pq.peek() > pqe.getLocation(curr_loc,dist)){
pq.poll();
}
}
});

pq.forEach(atmLoc -> System.out.println(atmLoc));
}

private double getLocation(double curr,double atm){
return atm - curr;
}
}``````

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

Assuming that getDistance(a, b) is O(1)...

We can iteratively quickselect the 1st closest ATM in the list, k times, by making comparisons based on each ATM's distance.

We can return the result in a list of size k.

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

He

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

Assuming getDistance() is O(1)...

Quickselect the 1st closest ATM, k times, and store the result in an array of size k.

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

Quickselect the Kth closest ATM. Loop through the list of ATMS and select all ATMS closer than the kth closest ATM that was previosuly selected.

Runtime O(2n) = O(n)
Space O(k)

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

Suppose you are in one location and they gave the 20 ATM's list, We have to invoke getDistance() 20 times to get the distance of all the ATM's, then do the sort and pick the first K locations? Am i right? O(1) for each ATM distance, O(n) for get all the distances and O(n) to select the K ATM centers, am i right?

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

we can use a K size queue to maintain k smallest distance ATMs or we can also use MAXHeap of size k to store k closet ATMs...I think to K space for K sized heap and it takes O(n) times to store them

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

PriorityQueue can also be used.

``````import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class PrioRQueueExample {

public static void main(String[] args){

PriorityQueue<Double> pq = new PriorityQueue<Double>((x,y)-> {Double z = y-x;return z.intValue(); });
PrioRQueueExample pqe = new PrioRQueueExample();

//Number of ATMs to return i.e. K
int num_ATMs = 3;

double curr_loc = 0.00;

Map<String,Double> nallATMLocs = new HashMap<String,Double>();

//Map of ATM names and their distance co-ordinates
nallATMLocs.put("atm1",45.0);
nallATMLocs.put("atm2",78.0);
nallATMLocs.put("atm3",54.0);
nallATMLocs.put("atm4",64.0);
nallATMLocs.put("atm5",35.0);
nallATMLocs.put("atm6",42.0);
nallATMLocs.put("atm7",57.0);
nallATMLocs.put("atm7",1.00);

nallATMLocs.forEach((atm,dist) ->{if(pq.size() < num_ATMs){
else{
if(	pq.peek() > pqe.getLocation(curr_loc,dist)){
pq.poll();
}
}
});

pq.forEach(atmLoc -> System.out.println(atmLoc));
}

private double getLocation(double curr,double atm){
return atm - curr;
}
}``````

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

jsahusu

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

hello ahmedthehack,

thanks for posting this, i've applied at amazon video uk as well and they've sent me this online coding assessment. can you kindly share the problems of your online coding assessment as well ?

thanks

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

hello ahmedthehack,

i've applied at amazon video uk as well and they've sent me an online technical test. Was wondering if you can share the problems your online technical test as well?

Thanks

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.