## Amazon Interview Question for SDE-2s

Country: United States

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

``````mybook={}

print("Enter the no of keys u want to enter")
n=int(input())

for i in range(0,n):
key=input("Enter the key")
value=input("Enter the value")

mybook[key]=value

print(mybook)

authors={}
key_list=[]
used=[]

key_list=mybook.keys()
for i in key_list:
v=mybook.get(i)
if v not in used:
used.append(v)
authors.setdefault(v,[])
authors[v].append(i)

print(authors)``````

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

static List<List<Integer>> ClosestXdestination(int numDestinations, List<List<Integer>> allocations, int numDeliveries) {
HashMap<List<Integer>,Integer> treemap = new HashMap<>();
List <List<Integer>> result = new ArrayList<>();
for (int i = 0; i < numDestinations; i++) {
double dist=Math.pow(allocations.get(i).get(0), 2)+Math.pow(allocations.get(i).get(1), 2);
treemap.put(allocations.get(i),(int) dist);
}
Map sortedMap = sortByValues(treemap);
Iterator i = sortedMap.entrySet().iterator();
int k=0;
// Display elements
while(i.hasNext() && k<numDeliveries) {
Map.Entry me = (Map.Entry)i.next();
k++;
}
return result;
}
// }

static <K, V extends Comparable<V>> Map<K, V> sortByValues(final Map<K, V> map) {
Map<K, V> sortedByValues = new TreeMap<>(new Comparator<K>() {
public int compare(K k1, K k2) {
int compare =map.get(k1).compareTo(map.get(k2));
if (compare == 0)
return 1;
else
return compare;
}
});
sortedByValues.putAll(map);
return sortedByValues;
}

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

``````List<List<Integer>> ClosestXdestinations(int numDestinations, List<List<Integer>> allLocations,
int numDeliveries) {
if (numDeliveries > numDestinations)
return allLocations;
TreeMap<Float, List<List<Integer>>> closestDestinations = new TreeMap<>();
for (List<Integer> point : allLocations) {
float dist = (float) Math.sqrt(point.get(0) * point.get(0) + point.get(1) * point.get(1));
List<List<Integer>> temp = new ArrayList<>();
if (closestDestinations.containsKey(dist))
temp = closestDestinations.get(dist);
closestDestinations.put(dist, temp);
}
ArrayList<List<Integer>> result = new ArrayList<>();
while (numDeliveries > 0) {
List<List<Integer>> temp = closestDestinations.get(closestDestinations.firstKey());
for (List<Integer> l : temp) {
numDeliveries--;
}
closestDestinations.remove(closestDestinations.firstKey());
}
return result;
}``````

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

``````public static List<List<int>> GetDeliveryPlan(int destinations, int deliveries, List<int[,]> Locations)
{
var deliveryPlan = new List<List<int>>();
var distances = new SortedDictionary<double, List<int>>();
// loop the locations
foreach (var l in Locations)
{
// calculate the distance and save it in to a SortedDictionary by using the distance as the Key
var dist = Math.Pow(l[0, 0], 2) + Math.Pow(l[0, 1], 2);
if (!distances.ContainsKey(dist))
distances.Add(dist, new List<int> {l[0, 0], l[0, 1]});
}
// loop the SortedDictionary and save the entries as deliveries in the deliveryPlan
var i = 0;
foreach (var d in distances)
{
i++;
if (i == deliveries)
return deliveryPlan;
}
return deliveryPlan;
}``````

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

``````public static List<List<int>> GetDeliveryPlan(int destinations, int deliveries, List<int[,]> Locations)
{
var deliveryPlan = new List<List<int>>();
var distances = new SortedDictionary<double, List<int>>();
// loop the locations
foreach (var l in Locations)
{
// calculate the distance and save it in to a SortedDictionary by using the distance as the Key
var dist = Math.Pow(l[0, 0], 2) + Math.Pow(l[0, 1], 2);
if (!distances.ContainsKey(dist))
distances.Add(dist, new List<int> {l[0, 0], l[0, 1]});
}
// loop the SortedDictionary and save the entries as deliveries in the deliveryPlan
var i = 0;
foreach (var d in distances)
{
i++;
if (i == deliveries)
return deliveryPlan;
}
return deliveryPlan;
}``````

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.