KatInterview

Find Members Missing a Nearby Provider Specialty

Arrays & HashingAuto-graded · 15 test cases
MediumAuto-gradedArrays & Hashinghashinggeometrygrid-bucketing

Last updated

Given providers and members on a 2D plane, find members with a required specialty that has no provider within a distance limit, using hashing and spatial grouping.

Find Members Missing a Nearby Provider Specialty

A health plan wants to check that each of its members can reach every kind of care they need without traveling too far.

Input

  • providers: a list of dictionaries, each with:
    • id: integer provider ID
    • specialty: string naming the provider's specialty
    • location: a pair [x, y] (or tuple) of coordinates
  • members: a list of dictionaries, each with:
    • id: integer member ID
    • location: a pair [x, y] of coordinates
    • required_specialties: a list of specialty strings the member needs
  • max_distance: a non-negative number, the farthest a member is willing to travel

Distances are straight-line (Euclidean) distances between points.

Task

A member is served if, for every specialty in their required_specialties, at least one provider of that specialty lies at distance less than or equal to max_distance from the member.

A member is underserved if at least one required specialty has no provider within that distance. This includes a specialty that no provider offers at all.

Return the IDs of all underserved members, in the same order in which those members appear in members.

Rules and edge cases

  • A distance exactly equal to max_distance counts as within range. Compare squared distance with the squared limit to avoid taking square roots.
  • If max_distance is 0, a provider is in range only if it sits at exactly the member's coordinates.
  • A member with an empty required_specialties list is always served.
  • A specialty may be repeated in a member's list, and several providers may share a location or specialty.
  • If every member is served, or members is empty, return an empty list.

Constraints

  • 0 <= len(providers), len(members) <= 10^5
  • Each member's required_specialties list is short (on the order of a few entries).
  • Coordinates are integers or floats of moderate magnitude.
  • max_distance >= 0
  • Member IDs are unique.

Examples

Example 1

Input: find_underserved_members([{'id': 1, 'specialty': 'Cardiology', 'location': [1, 2]}, {'id': 2, 'specialty': 'Dermatology', 'location': [3, 4]}], [{'id': 101, 'location': [2, 3], 'required_specialties': ['Cardiology', 'Dermatology']}, {'id': 102, 'location': [10, 10], 'required_specialties': ['Cardiology']}], 5)

Output: [102]

Hints

Hint 1

Group providers by specialty first, so a member's check for one specialty only looks at providers offering it.

Hint 2

Comparing every member against every provider of a specialty can be too slow. Bucket each specialty's providers into square grid cells whose side equals max_distance, so a nearby provider must lie in the member's own cell or one of the 8 neighbouring cells.

Hint 3

Handle max_distance == 0 separately: only an identical coordinate counts, so a hash lookup on the exact (x, y) point works. Compare squared distances against max_distance squared, and stop checking a member as soon as one specialty is unmet.