Frontend School

Maximizing Minimum Customer Satisfaction with Delivery Slots

DSA
hard

Maximizing Minimum Customer Satisfaction with Delivery Slots

Hard
Sorting
Searching
Binary Search
Greedy
Arrays
Priority Queue

Overview

Swiggy strives for exceptional customer satisfaction, especially during peak hours. A crucial aspect is assigning orders to available delivery slots such that customers are as happy as possible. Each order has a preferred delivery time and a flexibility window. We define customer satisfaction as 100 - |actualDeliveryTime - preferredDeliveryTime|, ensuring it's non-negative. We have a set of pending orders and a set of fixed delivery slots. Our goal is to assign as many orders as possible (up to the minimum of available orders and slots) to distinct slots, maximizing the minimum satisfaction score achieved across all assigned orders.

What you need to do / Task

Implement a function maxMinSatisfaction(orders, slots) that determines the highest possible minimum satisfaction score that can be achieved for a set of assigned orders.

Your function should:

  1. Take an array of orders, where each order is [preferredTime, flexibility].
  2. Take an array of slots, where each element is a slotTime.
  3. Assign at most min(number of orders, number of slots) orders to distinct slots.
  4. Maximize the minimum satisfaction score among all assigned orders.
  5. Return this maximum minimum satisfaction score.

Input & output

Function Signature: function maxMinSatisfaction(orders: number[][], slots: number[]): number

Parameters:

  • orders: An array of arrays, where orders[i] = [preferredTime_i, flexibility_i].
    • preferredTime_i: An integer representing the customer's ideal delivery time for order i.
    • flexibility_i: An integer representing the maximum acceptable deviation from preferredTime_i. An order can only be assigned to a slot s if abs(s - preferredTime_i) <= flexibility_i. This is a hard constraint.
  • slots: An array of integers, where slots[j] is the fixed time for the j-th available delivery slot.

Return Value:

  • An integer representing the maximum possible minimum satisfaction score. If no orders can be assigned, return 0.

Clarifications

  • The satisfaction score for an order delivered at actualTime is calculated as max(0, 100 - abs(actualTime - preferredTime)). This score must be non-negative, meaning abs(actualTime - preferredTime) cannot exceed 100 for any positive satisfaction.
  • Combining the two conditions: an order i can be assigned to a slot s if and only if abs(s - preferredTime_i) <= flexibility_i AND abs(s - preferredTime_i) <= (100 - S_min_target) (where S_min_target is the minimum satisfaction we are trying to achieve).
  • Each slot can be used for at most one order. Each order can be assigned to at most one slot.
  • If orders or slots are empty, or no assignments are possible, the minimum satisfaction is 0.

Examples

1

Example 1

Input:

orders = [[10, 5], [20, 5]], slots = [12, 18]

Output:

98

Note:

We need to assign min(2, 2) = 2 orders. Let's binary search for the minimum satisfaction S_min. If S_min = 98, the allowed deviation for satisfaction is 100 - 98 = 2.

  • Order 1: preferredTime=10, flexibility=5. Effective deviation for assignment is min(5, 2) = 2. So, valid slot range is [10-2, 10+2] = [8, 12].
  • Order 2: preferredTime=20, flexibility=5. Effective deviation for assignment is min(5, 2) = 2. So, valid slot range is [20-2, 20+2] = [18, 22]. Available orders for checking: [[8, 12], [18, 22]]. Available slots: [12, 18]. Using a greedy approach:
  1. Sort orders by start time: O1:[8,12], O2:[18,22].
  2. Sort slots: [12, 18].
  3. Initialize a min-priority queue pq (stores end times of available orders) and assigned_count = 0.
    • For slot 12: Add O1 (end=12) to pq. pq=[12]. Pop 12 from pq (as 12 >= 12). assigned_count = 1.
    • For slot 18: Add O2 (end=22) to pq. pq=[22]. Pop 22 from pq (as 22 >= 18). assigned_count = 2. Since assigned_count = 2, which is >= min(2,2), S_min=98 is achievable. If we try S_min = 99, the allowed deviation for satisfaction is 100 - 99 = 1.
  • Order 1: min(5, 1) = 1. Range [9, 11].
  • Order 2: min(5, 1) = 1. Range [19, 21]. Neither slot 12 nor 18 falls into these ranges. So S_min=99 is not achievable. Thus, the maximum minimum satisfaction is 98.
2

Example 2

Input:

orders = [[50, 10]], slots = [50]

Output:

100

Note:

We need to assign min(1, 1) = 1 order. If S_min = 100, the allowed deviation for satisfaction is 100 - 100 = 0.

  • Order 1: preferredTime=50, flexibility=10. Effective deviation is min(10, 0) = 0. Valid slot range: [50-0, 50+0] = [50, 50]. Available orders: [[50, 50]]. Available slots: [50]. Using greedy:
    • For slot 50: Add O1 (end=50) to pq. pq=[50]. Pop 50 from pq. assigned_count = 1. Since assigned_count = 1, which is >= min(1,1), S_min=100 is achievable. This is the maximum possible satisfaction, so the answer is 100.
3

Example 3

Input:

orders = [[10, 2], [30, 2]], slots = [11, 29]

Output:

99

Note:

We need to assign min(2, 2) = 2 orders. If S_min = 99, the allowed deviation for satisfaction is 100 - 99 = 1.

  • Order 1: preferredTime=10, flexibility=2. Effective deviation is min(2, 1) = 1. Valid slot range: [10-1, 10+1] = [9, 11].
  • Order 2: preferredTime=30, flexibility=2. Effective deviation is min(2, 1) = 1. Valid slot range: [30-1, 30+1] = [29, 31]. Available orders: [[9, 11], [29, 31]]. Available slots: [11, 29]. Using greedy:
    • For slot 11: Add O1 (end=11) to pq. pq=[11]. Pop 11 from pq. assigned_count = 1.
    • For slot 29: Add O2 (end=31) to pq. pq=[31]. Pop 31 from pq. assigned_count = 2. Since assigned_count = 2, which is >= min(2,2), S_min=99 is achievable. If we try S_min = 100, the allowed deviation for satisfaction is 0.
  • Order 1: min(2, 0) = 0. Range [10, 10].
  • Order 2: min(2, 0) = 0. Range [30, 30]. Neither slot 11 nor 29 falls into these very narrow ranges. So S_min=100 is not achievable. Thus, the maximum minimum satisfaction is 99.

Constraints

Constraints & assumptions

  • 1 <= orders.length, slots.length <= 10^5
  • 0 <= preferredTime_i, flexibility_i, slotTime_j <= 10^9
  • All times (preferredTime, slotTime) and flexibility are non-negative integers.
  • The satisfaction score is an integer between 0 and 100, inclusive.

What interviewers evaluate

  • Correctness: The solution must correctly calculate the maximum possible minimum satisfaction score.
  • Efficiency: The solution should be optimal in terms of time and space complexity, handling large inputs (10^5) within typical time limits (e.g., O(log(S_range) * (N log N + M log M)) overall).
  • Edge Cases: Handling scenarios like no possible assignments, empty inputs, or orders/slots that cannot meet any positive satisfaction.
  • Code Structure & Readability: Clear, well-organized, and maintainable code with appropriate data structures.

Loading editor...