Problem · Array

Open Restaurants in a City Range

Learn this problem
EasyDoorDash logoDoorDashFULLTIMEPHONE SCREEN

Problem statement

What the interview report shared

The interview report asked for open restaurants within a city range by using provided city and restaurant APIs to check both openness and range eligibility. Follow-up discussion covered edge cases and latency.

Task

A city range is described by an integer center (cityX, cityY) and a non-negative integer radius. Restaurant records are supplied through equal-length arrays: restaurantIds[i], restaurantX[i], restaurantY[i], and isOpen[i] describe one restaurant.

A restaurant is eligible exactly when isOpen[i] is true and its squared Euclidean distance from the city center is at most radius * radius.

Return the identifiers of all eligible restaurants in their original input order.

Function

findOpenRestaurants(cityX: int, cityY: int, radius: int, restaurantIds: String[], restaurantX: int[], restaurantY: int[], isOpen: boolean[]) → String[]

Examples

Example 1

cityX = 0cityY = 0radius = 5restaurantIds = ["alpha","bravo","charlie","delta"]restaurantX = [3,5,1,0]restaurantY = [4,1,1,6]isOpen = [true,true,false,true]return = ["alpha"]

alpha is open and lies exactly on the radius-five boundary. bravo and delta are outside the circle, while charlie is closed.

Example 2

cityX = 10cityY = -2radius = 3restaurantIds = ["north","center","west"]restaurantX = [10,10,7]restaurantY = [1,-2,-2]isOpen = [true,true,true]return = ["north","center","west"]

All three restaurants are open. The first and third lie exactly on the boundary, and the second is at the city center, so input order is preserved.

Example 3

cityX = 4cityY = 7radius = 0restaurantIds = []restaurantX = []restaurantY = []isOpen = []return = []

There are no restaurant records, so the result is empty.

Constraints

  • 0 <= restaurantIds.length <= 100000
  • restaurantX.length, restaurantY.length, and isOpen.length equal restaurantIds.length.
  • Every restaurant identifier is non-empty.
  • -10^6 <= cityX, cityY, restaurantX[i], restaurantY[i] <= 10^6
  • 0 <= radius <= 2 * 10^6

More DoorDash problems

drafts saved locally
public String[] findOpenRestaurants(int cityX, int cityY, int radius, String[] restaurantIds, int[] restaurantX, int[] restaurantY, boolean[] isOpen) {
    // Write your code here
}
cityX0
cityY0
radius5
restaurantIds["alpha","bravo","charlie","delta"]
restaurantX[3,5,1,0]
restaurantY[4,1,1,6]
isOpen[true,true,false,true]
expected["alpha"]
checking account