KatInterview

Share of Users Who Cancel and Never Come Back

Arrays & HashingAuto-graded · 19 test cases
EasyAuto-gradedArrays & Hashinghash-maptimestampsgrouping

Last 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 its scheduled_start_ts is 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.
  • status is 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.