KatInterview

Identify Customers With Unmet Service Needs

Arrays & HashingAuto-graded · 17 test cases
MediumAuto-gradedArrays & Hashingspatial-hashingset-operationsgrid-bucketing

Last updated

Tests spatial hashing and set union logic to find customers whose required service tags are not collectively offered by any nearby vendor within a radius.

Problem Statement

You are given a list of customers, a list of vendors, and a numeric range value. Each customer is situated at a 2D coordinate and needs a particular collection of service tags. Each vendor is located at a 2D coordinate and supplies a particular collection of service tags.

A vendor is within range of a customer when the Euclidean distance between their coordinates does not exceed range. Euclidean distance between (x1, y1) and (x2, y2) is sqrt((x1 - x2)^2 + (y1 - y2)^2).

A customer is fully served when every service tag they need appears in the combined offerings of all vendors within range. Tags can come from different vendors — no single vendor must supply all of them.

A customer is underserved when at least one needed tag is missing from every vendor within range. Return the IDs of all underserved customers in the same order they appear in the input.

Input Format

Each element of customers is a dict with:

  • 'id': an integer identifier
  • 'coords': a list or tuple [x, y] of integer coordinates
  • 'specialties': a set or list of string tags the customer needs

Each element of vendors is a dict with:

  • 'id': an integer identifier
  • 'coords': a list or tuple [x, y] of integer coordinates
  • 'specialties': a set or list of string tags the vendor offers

range is a float or int.

Edge Case Rules

  • A customer whose specialties is empty needs nothing and is never underserved.
  • If range is negative, no vendor can ever be within range, so every customer with at least one needed tag is underserved.
  • If range is zero, only vendors at the exact same coordinates as the customer count as within range.
  • Distance comparison is inclusive: a vendor at exactly range distance is within range.

Constraints

  • Number of customers and vendors: up to 10,000 each.
  • Coordinates: integers in the range [-10,000, 10,000].
  • Service tags: non-empty strings.
  • range: any numeric value (including zero and negatives).
  • The returned list must preserve the original ordering of customers.

Examples

Example 1

Input: find_uncovered_clients([{'id': 1, 'coords': [0, 0], 'specialties': ['Cardio', 'Dentist']}], [{'id': 10, 'coords': [1, 1], 'specialties': ['Cardio']}], 2)

Output: [1]

Example 2

Input: find_uncovered_clients([{'id': 2, 'coords': [0, 0], 'specialties': ['Eye']}], [{'id': 11, 'coords': [0, 1], 'specialties': ['Eye']}], 2)

Output: []

Hints

Hint 1

Pairing every customer with every vendor is O(C·V) which can reach 10^8 comparisons. Consider bucketing vendors by location to prune candidates.

Hint 2

A uniform grid with cell side length equal to `range` guarantees any vendor within distance `range` of a customer lies in one of the 9 cells surrounding the customer's own cell. Use `math.floor(coord / range)` for cell keys, not `int()` which truncates toward zero for negatives.

Hint 3

Coverage is a union across vendors. Track the set of still-unmet tags per customer, subtract each nearby vendor's tags, and stop early when the set is empty. Handle `range == 0` separately to avoid division by zero by grouping vendors by exact coordinate.