Interview Query

Location Frequency

0
Have you seen this question before?

You work at a freight shipping company with a large fleet of trucks. These trucks are based around the nation, with a mix of Mercedes and BMW models represented. The location of each truck that your company owns is stored as (x,y) coordinates in your database.

Given a list of locations that your trucks are stored at, return the top location (x,y) for each model of truck (Mercedes or BMW).

You can assume that there is one location with the most model of a truck, e.g. there will not be any locations that are tied for most BMWs stored.

Example:

Input:

truck_locations = [{
    "model" : "BMW",
    "location" : (1,2)
  },
  {
    "model" : "Mercedes",
    "location" : (2,3)
  },
  {
    "model" : "Mercedes",
    "location" : (2,2)
  },
  {
    "model" : "Mercedes",
    "location" : (2,3)
  },
  {
    "model" : "BMW",
    "location" = (1,2)
  },
  {
    "model" : "BMW",
    "location" : (3,3)
  }
  ]

Output:

def truck_frequent_location(truck_locations) -> {    
    "Mercedes" :  (2,3),   
    "BMW" :  (1,2)   
    }
}

Explanation:

Mercedes repeated two times at location (2,3) and one time at the location (2,2), so (2,3) is our most stored Mercedes location.

BMW repeated two times at location (1,2) and one time at the location (3,3), so (1,2) is our most stored BMW location.

Next question: Yelp-like System
.....
Python 3.9.6
Loading editor
Use Shift + Enter to run code