Amazon
  • Introduction
  • Phone Interview I
    • 53.Reverse Words in a String
    • 31.Partition Array
  • Phone Interview II
    • 167.Add Two Numbers
    • 88.Lowest Common Ancestor
  • Onsite I
    • 655.Big Integer Addition
    • 221.Add Two Numbers II
  • Onsite II
    • 158.Two Strings Are Anagrams
    • 171.Anagrams
    • 386.Longest Substring with At Most K Distinct Characters
  • Onsite III
    • 479.Second Max of Array
    • 589.Connecting Graph
  • Onsite IV
    • 532.Reverse Pairs
  • 2022
    • OA
      • work simulation
      • Greyness
      • NearestRetailer
      • Sum of Scores of Subarray
      • StrengthOfPassword
      • ProductOf1
      • Move 0/1 InArray
      • Max deviation among all substrings
      • AWS power consumption
      • searchWordResultWord
      • maxOperationOfString
      • MinHealthGame
      • EarliestMonth
      • Package ship
      • RatingConsectiveDecresing
      • LinkedListSum
      • MovingBoxes
      • ValidString
      • MaxValueAfterRemovingFromString
      • Subtree with Maximum Average
    • VO
      • 2022.3
    • BQ
      • doc
      • 2022.4
      • Freq Question
      • 11大类BQ和Follow-ups
      • Page 1
      • BQ 100
      • 35 behavioral questions asked in 95% of Amazon interviews with examples
      • Page 2
      • 反向BQ
    • LP
      • LP-1
      • LP-2
    • SD
      • Design Amazon Prime Video Recommendation System
      • Amazon Order system
    • OOD
      • Linux Find Command
      • Amazon Locker
    • AWS Identity call
    • Interviews
Powered by GitBook
On this page

Was this helpful?

  1. 2022
  2. OA

NearestRetailer

https://leetcode.com/discuss/interview-question/1692516/Amazon-OA1-full-time-question-Jan-2022

PreviousGreynessNextSum of Scores of Subarray

Last updated 3 years ago

Was this helpful?

Solution:

Using a Treemap and taking advantage of the fact that y is between 1 and 100. I bucket sort it in a treemap, treemap functions are log(n) but turn into log(100). Possibly missed edge cases but the happy path seems to work and the logic makes sense. O(nlogn) I think, comments are in the code, the formatting is messed up but not sure how to change it. There may be an easier way too, but I think the key is abusing the fact that the y values are so low, since there's no other reason they would have made the condition 1<=request[i][1]<=100.

// "static void main" must be defined in a public class.
public class Main {
public static void main(String[] args) {
int [][] retailers = new int[][]{{1,2},{2,3},{1,5}};
int [][] requests = new int[][]{{1,1},{1,4}};
System.out.println(Arrays.toString(countNumberOfRetailers(retailers,requests)));
}
public static int [] countNumberOfRetailers(int [][] retailers, int[][] requests){
//bucket sort since we know y is between 1 and 100
TreeMap<Integer, List<int[]>> map = new TreeMap<>();
Arrays.sort(retailers, (a,b)->{
return b[1]-a[1];
});
ArrayList<int[]> current = new ArrayList<>();
//keep a running total of valid retailers at each existing Y
//clone can only be called at most 100 times so amortized 100*N+O(nlogn) since there can be only at most 100 keys (different y coordinates)
for(int[] retailer: retailers){
int y = retailer[1];
List<int[]> temp = map.get(y);
if(!map.containsKey(y)){
temp = (ArrayList)current.clone();
}
temp.add(retailer);
current.add(retailer);
map.put(y, temp);
}

    //at most 100*nlogn
    for(Map.Entry<Integer,List<int[]>> entry: map.entrySet()){
        List<int[]> list = entry.getValue();
        //sort by x
        Collections.sort(list, (a,b)->{
            return a[0] - b[0]; 
        });
        map.put(entry.getKey(),list);
    }
    
    //now for each request we can grab a list of valid retailers eliminating y and binary search the sorted list O(logn)
    int [] res = new int[requests.length];
    for(int i=0; i<requests.length;++i){
        int [] request = requests[i];
        List<int[]> sortedList = map.ceilingEntry(request[1]).getValue();
        //System.out.println(sortedList.size()+" "+Arrays.toString(request));
        //binary search
        int start = 0;
        int end = sortedList.size()-1;
        int x = request[0];
        while(start<end){
            int mid = start+(end-start)/2;
            if( x < sortedList.get(mid)[0]){
                start=mid+1;
            }else{
                end = mid;
            }
        }
        res[i] = sortedList.size()-start;
    }
    return res;
    
}
}

https://leetcode.com/discuss/interview-question/1692516/Amazon-OA1-full-time-question-Jan-2022