Amazon Interview Question for Software Engineers


Country: United States
Interview Type: Phone Interview




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

SELECT city.name, COUNT(*) as count FROM orders LEFT JOIN city ON orders.city=city.id GROUP BY orders.city ORDER BY count DESC LIMIT N

- dmitry.labutin February 09, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

dmitry.labutin is right, of course - Or use a heap.

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

use hash table to get the count by city by going throught the list of cities and then sort the result using heap

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

bnb

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

mnm

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class MostOrders {
	
	public static List<Entry<String,Integer>> topNCities(Map inputMap,int n){
		
		//inputMap : Map of city to number of orders
		Set<Entry<String,Integer>> entrySet = inputMap.entrySet();
		
		List<Entry<String,Integer>> list = new ArrayList<Entry<String,Integer>>(entrySet);
		
		Collections.sort(list,new Comparator<Map.Entry<String, Integer>>() {

			@Override
			public int compare(Entry<String, Integer> e1, Entry<String, Integer> e2) {
				
				return (e2.getValue()).compareTo(e1.getValue());
			}
		});
		
		List<Entry<String,Integer>> outputList = new ArrayList<Entry<String,Integer>>();
		//count top n cities
		int count = 0;
		
		
		for(Map.Entry<String, Integer> entry:list){		
			count++;
			if(count<=n)
				outputList.add(entry);
			//System.out.println(entry.getKey()+" == "+entry.getValue());
		}
		
		return outputList;
	}
	
	

	public static void main(String[] args){
		Map<String,Integer> inputMap = new HashMap<String,Integer>();
		inputMap.put("Austin", 65);
		inputMap.put("Houston", 30);
		inputMap.put("Chicago",50);
		inputMap.put("New Your", 100);
		inputMap.put("Colarado", 20);
		
		List<Entry<String, Integer>> output = topNCities(inputMap, 2);
		System.out.println(output);
		
	}
}

- chetan.nagarajrao February 21, 2017 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorting-free optimal solution. O(n) time. Use a priority queue to solve the problem. Perfectly dealt with the cases when at rank 10 there are multiple cities with the same number of orders.

import java.util.*;

class CityOrder {
    private String city;
    int orderCount;

    public CityOrder(String city) {
        this.city = city;
        orderCount = 1;
    }
}
public class TopNOrderCities {

    private PriorityQueue<CityOrder> topN = new PriorityQueue<>((o1, o2) -> (o1.orderCount - o2.orderCount));   //keep top n cities with most orders in here
    private Set<CityOrder> minOrderInTopNCities = new HashSet<>();
    private Map<String, CityOrder> cityOrderMap = new HashMap<>();

    public TopNOrderCities(int n, String[] orders) {
        for(String city: orders) {
            CityOrder cityOrder;

            if(!cityOrderMap.containsKey(city)) {
                cityOrder = new CityOrder(city);
                cityOrderMap.put(city, cityOrder);

                if(topN.size() < n) {
                    topN.add(cityOrder);
                    cityOrderMap.put(city, cityOrder);
                } else if(topN.peek().orderCount == 1) {
                    minOrderInTopNCities.add(cityOrder);
                }
            } else {
                cityOrder = cityOrderMap.get(city);
            }

            CityOrder min = topN.peek();
            if(cityOrder.orderCount == min.orderCount) {
                minOrderInTopNCities.add(cityOrder);
            } else if(cityOrder.orderCount == min.orderCount + 1) {

                if(minOrderInTopNCities.contains(cityOrder)) {
                    minOrderInTopNCities.remove(cityOrder);
                    topN.add(cityOrder);
                }
                CityOrder temp;
                if(topN.size() == n + 1) {
                     temp = topN.poll();
                     if(topN.peek().orderCount == cityOrder.orderCount) {
                         minOrderInTopNCities = new HashSet<>();
                     } else {
                         minOrderInTopNCities.add(temp);
                     }
                }
                if(topN.peek().orderCount == cityOrder.orderCount) {
                    minOrderInTopNCities = new HashSet<>();
                }

                min = topN.poll();
                if(cityOrder.orderCount == topN.peek().orderCount && minOrderInTopNCities.contains(cityOrder)) {
                    minOrderInTopNCities.remove(cityOrder);
                    topN.add(cityOrder);

                }
            }
        }
    }


}

Looking for interview questions sharing and mentors? Visit A++ Coding Bootcamp at aonecode.com (select english at the top right corner).
We provide ONE TO ONE courses that cover everything in an interview from the latest interview questions, coding, algorithms, system design to mock interviews. All classes are given by experienced engineers/interviewers from FB, Google and Uber. Help you close the gap between school and work. Our students got offers from G, U, FB, Amz, Yahoo and other top companies after a few weeks of training.
Welcome to email us with any questions.

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

This problem can be easily solved using map and priority queue.as map has key and value pair in key we will store city and value will store number of orders.in this way we have hash the city according to the number of order.now we will use priority queue to store this result.the city with more number of order will be a top and city with less number of order will be at bottom.

- Anonymous April 07, 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