Share of Users Who Cancel and Never Come Back
Arrays & HashingAuto-graded · 19 test casesLast updated
Group booking records by user, find each canceller's earliest cancellation, and compute the percentage with no later confirmed or returned booking.
You are given a log of bookings made in a scheduling app. Each booking is a dictionary with string values:
appointment_id: unique id of the booking.user_id: the user who owns the booking.scheduled_start_ts: the planned start time as a UTC ISO-8601 timestamp, such as"2024-01-01T10:00:00Z".status: one of"confirmed","cancelled", or"returned".
Each booking appears exactly once, carrying its final status. The list is not guaranteed to be in any particular order.
Definitions
- A user's anchor cancellation is their cancelled booking with the earliest
scheduled_start_ts. If several cancelled bookings share that earliest time, they all give the same anchor time. - A booking is a comeback for a user if it belongs to the same user, its status is
"confirmed"or"returned", and itsscheduled_start_tsis strictly later than the anchor cancellation's time.
Task
Among all users who have at least one cancelled booking, compute the percentage whose anchor cancellation has no comeback. Return
100 * (users with an anchor and no comeback) / (users with an anchor)
as a float.
Rules
- Users with no cancelled bookings are ignored entirely.
- A confirmed or returned booking at exactly the same time as the anchor cancellation is not a comeback.
- Confirmed or returned bookings earlier than the anchor do not count as comebacks.
- Later bookings that are themselves cancelled do not count as comebacks.
- Only the anchor matters: a comeback that falls between two cancellations still counts, because it is later than the anchor.
- A user with several comebacks is counted only once.
- If no user has a cancelled booking, return
0.0. - Do not round the result.
Constraints
0 <= len(appointments) <= 10^5- All timestamps are valid UTC ISO-8601 strings ending in
Z. statusis always one of the three listed values.- Compare timestamps as points in time, not as raw text.
Examples
Example 1
Input: cancel_no_return_percent([{'appointment_id': 'a1', 'user_id': 'u1', 'scheduled_start_ts': '2024-01-01T10:00:00Z', 'status': 'cancelled'}, {'appointment_id': 'a2', 'user_id': 'u1', 'scheduled_start_ts': '2024-01-02T10:00:00Z', 'status': 'confirmed'}, {'appointment_id': 'a3', 'user_id': 'u2', 'scheduled_start_ts': '2024-01-01T09:00:00Z', 'status': 'cancelled'}, {'appointment_id': 'a4', 'user_id': 'u2', 'scheduled_start_ts': '2024-01-03T09:00:00Z', 'status': 'cancelled'}])
Output: 50
Example 2
Input: cancel_no_return_percent([{'appointment_id': 'a1', 'user_id': 'u1', 'scheduled_start_ts': '2024-01-01T10:00:00Z', 'status': 'cancelled'}, {'appointment_id': 'a2', 'user_id': 'u1', 'scheduled_start_ts': '2024-01-01T10:00:00Z', 'status': 'returned'}])
Output: 100
Example 3
Input: cancel_no_return_percent([{'appointment_id': 'a1', 'user_id': 'u1', 'scheduled_start_ts': '2024-01-01T10:00:00Z', 'status': 'confirmed'}, {'appointment_id': 'a2', 'user_id': 'u2', 'scheduled_start_ts': '2024-01-02T10:00:00Z', 'status': 'returned'}])
Output: 0
Example 4
Input: cancel_no_return_percent([{'appointment_id': 'a1', 'user_id': 'u1', 'scheduled_start_ts': '2024-01-03T10:00:00Z', 'status': 'cancelled'}, {'appointment_id': 'a2', 'user_id': 'u1', 'scheduled_start_ts': '2024-01-01T10:00:00Z', 'status': 'cancelled'}, {'appointment_id': 'a3', 'user_id': 'u1', 'scheduled_start_ts': '2024-01-02T10:00:00Z', 'status': 'returned'}])
Output: 0
Hints
Hint 1
Start by finding, for each user with a cancellation, the earliest cancelled start time. The users in this map form your denominator.
Hint 2
Make a second pass over the bookings: for a user in that map, a confirmed or returned booking with a start time strictly after the anchor means the user came back. Record such users in a set so each is counted once.
Hint 3
Parse the timestamps into datetime objects before comparing, use a strict greater-than, and return 0.0 early when nobody cancelled to avoid dividing by zero.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The answer depends on two facts per user:
- The anchor time: the earliest
scheduled_start_tsamong the user's cancelled bookings. Only users who have one appear in this map, so users without cancellations are automatically excluded from the denominator. - Whether a comeback exists: any confirmed or returned booking for that user with a start time strictly after the anchor.
The result is 100 * (anchored users with no comeback) / (anchored users).
Steps:
- Pass 1: scan the bookings and, for each cancelled one, keep the minimum parsed timestamp per user in a dictionary.
- If the dictionary is empty, return
0.0. - Pass 2: for each booking whose user is in the dictionary, whose status is confirmed or returned, and whose time is strictly greater than the user's anchor, add the user to a set.
- Return
100.0 * (anchored users - users in the set) / anchored users.
Why it is correct
- The dictionary has exactly one entry per user with at least one cancellation, which is the required denominator.
- Taking the minimum gives the earliest cancellation, and ties just produce the same anchor.
- The strict
>comparison excludes bookings at the same instant as the anchor. The status filter excludes cancelled bookings, and the time filter excludes earlier ones. - Only the anchor is used, so a comeback between two cancellations is still a comeback.
- Using a set means several comebacks for one user count once.
- Parsing to
datetimemakes the comparison about actual instants rather than string order.
Complexity
- Time: O(n), with two linear passes and constant-time dictionary and set operations.
- Space: O(u), where u is the number of users with at least one cancellation.
Reference solution
from datetime import datetime
from typing import Dict, List
def cancel_no_return_percent(appointments: List[Dict[str, str]]) -> float:
def parse_timestamp(value: str) -> datetime:
return datetime.fromisoformat(value.replace("Z", "+00:00"))
first_cancelled = {}
for appointment in appointments:
if appointment["status"] != "cancelled":
continue
user_id = appointment["user_id"]
timestamp = parse_timestamp(appointment["scheduled_start_ts"])
if user_id not in first_cancelled or timestamp < first_cancelled[user_id]:
first_cancelled[user_id] = timestamp
denominator = len(first_cancelled)
if denominator == 0:
return 0.0
returned_users = set()
for appointment in appointments:
user_id = appointment["user_id"]
if user_id not in first_cancelled:
continue
if appointment["status"] not in ("returned", "confirmed"):
continue
if parse_timestamp(appointment["scheduled_start_ts"]) > first_cancelled[user_id]:
returned_users.add(user_id)
never_returned = denominator - len(returned_users)
return 100.0 * never_returned / denominator