Design a hotel service with CRUD operations and Haversine radius search

Quick Overview

Design a hotel service that supports add, update, delete and lookup by id, plus a search that returns every hotel within a radius of a coordinate, ordered by great-circle distance and then id. It tests hash-map indexing, the Haversine formula, deterministic sorting and knowing when a linear scan needs a spatial index.

Design a hotel service with CRUD operations and Haversine radius search

Company: Booking.com

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a `HotelService` that manages a collection of hotels and supports CRUD operations along with proximity-based search. Each hotel has a unique `hotelId`, a `name`, a geographic location (`latitude`, `longitude`) and a nightly `price`. The service should be able to add new hotels, update existing ones, remove them, and find all hotels within a given radius (in kilometers) of a target coordinate. For proximity search, use the Haversine formula to compute the great-circle distance between two points on Earth (radius 6371 km). Results must be returned in ascending order of distance, with `hotelId` as a tiebreaker. Implement the following methods: - `addHotel(hotelId: int, name: str, latitude: float, longitude: float, price: float) -> bool` adds a new hotel. It returns `True` if the hotel was added and `False` if a hotel with the same id already exists. - `updateHotel(hotelId: int, name: str, latitude: float, longitude: float, price: float) -> bool` updates an existing hotel's fields. It returns `True` if the hotel was updated and `False` if it does not exist. - `deleteHotel(hotelId: int) -> bool` deletes a hotel by id. It returns `True` if the hotel was deleted and `False` if it was not found. - `getHotel(hotelId: int) -> list` returns `[hotelId, name, latitude, longitude, price]` for the given id, or an empty list if it is not found. - `searchNearby(latitude: float, longitude: float, radiusKm: float) -> list` returns the ids of the hotels within `radiusKm` of the given coordinate, sorted by ascending distance and then ascending `hotelId`. Example 1: ```text Input: ["HotelService", "addHotel", "addHotel", "addHotel", "addHotel", "searchNearby", "updateHotel", "deleteHotel", "searchNearby", "getHotel"] [[], [1, "Harbor", 0, 0, 120], [2, "Hill", 0, 1, 90], [3, "Twin", 0, 0.5, 200], [4, "Corner", 0, 0.5, 80], [0, 0, 120], [2, "Hill", 0, 2, 95], [3], [0, 0, 120], [3]] Output: [null, true, true, true, true, [1, 3, 4, 2], true, true, [1, 4], []] ``` Hotel 1 is at the query point, 0 km away. Hotels 3 and 4 share a location half a degree of longitude east along the equator, about 55.6 km away, so they tie on distance and appear in id order. Hotel 2 is one degree east, about 111.2 km away. After hotel 2 moves to two degrees east (about 222.4 km) and hotel 3 is deleted, only hotels 1 and 4 remain within 120 km, and `getHotel(3)` returns an empty list. Example 2: ```text Input: ["HotelService", "addHotel", "addHotel", "deleteHotel", "updateHotel", "getHotel", "searchNearby"] [[], [10, "A", 0, 0, 100], [10, "Duplicate", 1, 1, 150], [99], [99, "Ghost", 5, 5, 50], [10], [0, 0, 1000]] Output: [null, true, false, false, false, [10, "A", 0.0, 0.0, 100.0], [10]] ``` The second `addHotel` fails because id 10 already exists, and the original record stays unchanged. Deleting or updating id 99 fails because that hotel was never added. Hotel 10 sits at the query point, so the search returns it. ```hint Index by what the calls use Four of the five methods look a hotel up by `hotelId`. Pick a structure that makes those calls constant time, and treat the search as a separate problem. ``` ```hint Distance pitfalls The trigonometric functions take radians, and floating-point rounding can push the value under the square root slightly above 1 for points on nearly opposite sides of the Earth. ``` ### Constraints & Assumptions - `1 <= hotelId <= 10^6` - `-90 <= latitude <= 90` - `-180 <= longitude <= 180` - `0 <= price <= 10^5` - `0 < radiusKm <= 20000` - Distances use the Haversine formula with an Earth radius of 6371 km. ### Clarifying Questions to Ask - Does a hotel exactly `radiusKm` away count as within the radius? - Roughly how many hotels will the service hold, and how often is `searchNearby` called compared with writes? - Does `updateHotel` always replace every field, or should it support changing only some of them? - Will several threads call the service at the same time? ### What a Strong Answer Covers - A primary map keyed by `hotelId`, with the exact return value for a duplicate add, a missing update or delete, and a missing lookup. - A correct Haversine distance (radians, the 6371 km radius, the numerical edge near antipodal points) and an explicit choice about the radius boundary. - Sorting by distance and then `hotelId` so the result order is fully determined. - The time and space cost of each method, and the point where scanning every hotel on each search stops being acceptable. ### Follow-up Questions - With millions of hotels and searches far outnumbering writes, how would you index locations so a search does not touch every hotel? - How would you return only the k nearest hotels, and what does that change in the cost? - How would you add a maximum-price filter to `searchNearby`? - How would you make the service safe when several threads add, update and search at once?

Overview: Design a hotel service that supports add, update, delete and lookup by id, plus a search that returns every hotel within a radius of a coordinate, ordered by great-circle distance and then id. It tests hash-map indexing, the Haversine formula, deterministic sorting and knowing when a linear scan needs a spatial index.

Booking.com logo
Booking.com
Nov 24, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a HotelService that manages a collection of hotels and supports CRUD operations along with proximity-based search.

Each hotel has a unique hotelId, a name, a geographic location (latitude, longitude) and a nightly price. The service should be able to add new hotels, update existing ones, remove them, and find all hotels within a given radius (in kilometers) of a target coordinate.

For proximity search, use the Haversine formula to compute the great-circle distance between two points on Earth (radius 6371 km). Results must be returned in ascending order of distance, with hotelId as a tiebreaker.

Implement the following methods:

  • addHotel(hotelId: int, name: str, latitude: float, longitude: float, price: float) -> bool adds a new hotel. It returns True if the hotel was added and False if a hotel with the same id already exists.
  • updateHotel(hotelId: int, name: str, latitude: float, longitude: float, price: float) -> bool updates an existing hotel's fields. It returns True if the hotel was updated and False if it does not exist.
  • deleteHotel(hotelId: int) -> bool deletes a hotel by id. It returns True if the hotel was deleted and False if it was not found.
  • getHotel(hotelId: int) -> list returns [hotelId, name, latitude, longitude, price] for the given id, or an empty list if it is not found.
  • searchNearby(latitude: float, longitude: float, radiusKm: float) -> list returns the ids of the hotels within radiusKm of the given coordinate, sorted by ascending distance and then ascending hotelId .

Example 1:

Input:
["HotelService", "addHotel",                 "addHotel",              "addHotel",                "addHotel",                  "searchNearby", "updateHotel",           "deleteHotel", "searchNearby", "getHotel"]
[[],             [1, "Harbor", 0, 0, 120],   [2, "Hill", 0, 1, 90],   [3, "Twin", 0, 0.5, 200],  [4, "Corner", 0, 0.5, 80],   [0, 0, 120],    [2, "Hill", 0, 2, 95],   [3],           [0, 0, 120],    [3]]

Output: [null, true, true, true, true, [1, 3, 4, 2], true, true, [1, 4], []]

Hotel 1 is at the query point, 0 km away. Hotels 3 and 4 share a location half a degree of longitude east along the equator, about 55.6 km away, so they tie on distance and appear in id order. Hotel 2 is one degree east, about 111.2 km away. After hotel 2 moves to two degrees east (about 222.4 km) and hotel 3 is deleted, only hotels 1 and 4 remain within 120 km, and getHotel(3) returns an empty list.

Example 2:

Input:
["HotelService", "addHotel",           "addHotel",                   "deleteHotel", "updateHotel",           "getHotel", "searchNearby"]
[[],             [10, "A", 0, 0, 100], [10, "Duplicate", 1, 1, 150], [99],          [99, "Ghost", 5, 5, 50], [10],       [0, 0, 1000]]

Output: [null, true, false, false, false, [10, "A", 0.0, 0.0, 100.0], [10]]

The second addHotel fails because id 10 already exists, and the original record stays unchanged. Deleting or updating id 99 fails because that hotel was never added. Hotel 10 sits at the query point, so the search returns it.

Constraints & Assumptions

  • 1 <= hotelId <= 10^6
  • -90 <= latitude <= 90
  • -180 <= longitude <= 180
  • 0 <= price <= 10^5
  • 0 < radiusKm <= 20000
  • Distances use the Haversine formula with an Earth radius of 6371 km.

Clarifying Questions to Ask Guidance

  • Does a hotel exactly radiusKm away count as within the radius?
  • Roughly how many hotels will the service hold, and how often is searchNearby called compared with writes?
  • Does updateHotel always replace every field, or should it support changing only some of them?
  • Will several threads call the service at the same time?

What a Strong Answer Covers Guidance

  • A primary map keyed by hotelId , with the exact return value for a duplicate add, a missing update or delete, and a missing lookup.
  • A correct Haversine distance (radians, the 6371 km radius, the numerical edge near antipodal points) and an explicit choice about the radius boundary.
  • Sorting by distance and then hotelId so the result order is fully determined.
  • The time and space cost of each method, and the point where scanning every hotel on each search stops being acceptable.

Follow-up Questions Guidance

  • With millions of hotels and searches far outnumbering writes, how would you index locations so a search does not touch every hotel?
  • How would you return only the k nearest hotels, and what does that change in the cost?
  • How would you add a maximum-price filter to searchNearby ?
  • How would you make the service safe when several threads add, update and search at once?