PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates combinatorial optimization and constrained matching skills, focusing on reasoning about distance-based pairings, lexicographic objective prioritization, and edge-case handling such as duplicates and empty inputs.

  • medium
  • Google
  • Coding & Algorithms
  • Software Engineer

Maximize 1D deliveries within distance and minimize total distance

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given positions on a 1D line: - `people`: an array of `N` integers (positions of people) - `shops`: an array of `M` integers (positions of milk-tea shops) You may match a shop to a person if the delivery distance is within a fixed limit `D`: \[ |shops[k] - people[i]| \le D \] Constraints: - Each shop can deliver to **at most one** person. - Each person can receive from **at most one** shop. ### Objective (lexicographic) 1. **Maximize** the number of deliveries (matched pairs). 2. Among all matchings that achieve the maximum number of deliveries, **minimize** the **total** delivery distance: \[ \sum |shops[k] - people[i]| \] ### Task Given `people`, `shops`, and `D` (e.g., `D = 5`), return: - `maxDeliveries`: the maximum number of deliveries possible - `minTotalDistance`: the minimum total distance among matchings that achieve `maxDeliveries` ### Notes - Positions may be unsorted; you may sort them. - Specify and handle edge cases such as duplicates, empty arrays, and when no pairs are feasible.

Quick Answer: This question evaluates combinatorial optimization and constrained matching skills, focusing on reasoning about distance-based pairings, lexicographic objective prioritization, and edge-case handling such as duplicates and empty inputs.

You are given two arrays of integer positions on a 1D number line: `people` and `shops`, along with a non-negative integer `D`. A shop can deliver to a person only if their distance is at most `D`, meaning `abs(shops[k] - people[i]) <= D`. Each shop can serve at most one person, and each person can receive from at most one shop. Your task is to return a tuple `(maxDeliveries, minTotalDistance)` where `maxDeliveries` is the largest number of valid shop-person matches possible, and `minTotalDistance` is the smallest possible sum of delivery distances among all matchings that achieve `maxDeliveries`. If no deliveries are possible, return `(0, 0)`. The input arrays may be unsorted and may contain duplicates.

Constraints

  • 0 <= len(people), len(shops) <= 2000
  • -10^9 <= people[i], shops[j] <= 10^9
  • 0 <= D <= 10^9
  • Positions may be unsorted and may contain duplicates

Examples

Input: ([1, 7, 10], [2, 6, 12], 2)

Expected Output: (3, 4)

Explanation: All three people can be matched: 1->2, 7->6, and 10->12 with distances 1, 1, and 2. That gives 3 deliveries with total distance 4.

Input: ([0, 4], [2, 5], 3)

Expected Output: (2, 3)

Explanation: The best matching is 0->2 and 4->5. This achieves the maximum 2 deliveries with total distance 2 + 1 = 3.

Input: ([5, 5, 10], [4, 6, 11], 1)

Expected Output: (3, 3)

Explanation: A valid optimal matching is 5->4, 5->6, and 10->11. All 3 deliveries are made, and the total distance is 1 + 1 + 1 = 3.

Input: ([-3, 0, 4], [-4, 1], 1)

Expected Output: (2, 2)

Explanation: Match -3->-4 and 0->1. The person at 4 cannot be served within distance 1. So the result is 2 deliveries with total distance 2.

Input: ([], [1, 2, 3], 5)

Expected Output: (0, 0)

Explanation: There are no people to serve, so no deliveries can be made.

Input: ([0, 100], [10, 90], 5)

Expected Output: (0, 0)

Explanation: No shop is within distance 5 of any person, so no valid pair exists.

Hints

  1. Sort both arrays first. On a 1D line, there is always an optimal solution with no crossing matches.
  2. Use dynamic programming on prefixes: for the first `i` people and first `j` shops, store the best pair `(deliveries, totalDistance)` in lexicographic order.
Last updated: Apr 23, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Solve Rooms and Top-K Streams - Google (medium)
  • Find Containing Range - Google (medium)
  • Rearrange Tasks With Cooldown - Google (medium)
  • Implement Employee Management and Expression Evaluation - Google (medium)
  • Solve Three Array and Matrix Path Problems - Google (medium)