cswork / libraryOpen practice workspace

Coding questions · Stripe

Generate user notifications from schedules

This question evaluates skills in data manipulation and integration, including schema design, merging event and user profile data, recurring event handling, timezone and locale-aware conversions, conflict resolution, and data validation using SQL and Python.

Given a schedule template format and user profile data (timezone, locale, delivery preferences), implement a program that generates a user notification sheet. The program should merge event data with user information, handle recurring events, perform correct timezone conversions, localize date/time and text, resolve conflicting or overlapping events, and validate missing or malformed fields. Specify input and output schemas, show example transformations, and discuss the time and space complexity of your approach.

Generate User Notification Sheet from Schedules

Write a PostgreSQL query. You are given tables describing users, schedule templates, user-specific schedule assignments, and localized message texts. For a single delivery date 2025-06-01, write a SQL query that generates a user notification sheet.

Each schedule template represents a daily recurring notification; the column base_time_utc stores the UTC send-time for the 2025-06-01 occurrence of that template.

Your query should, for the delivery date 2025-06-01:

  1. Combine users with the schedule templates assigned to them that are active on that date (based on start_date/end_date and active_flag).
  2. Convert the base_time_utc to each user's local time using their timezone_offset_minutes (minutes to add to UTC to get local time).
  3. Determine a single delivery_channel per user: use 'push' if allow_push is TRUE; otherwise use 'email' if allow_email is TRUE; if both are FALSE, the user should receive no notifications.
  4. Localize the notification text by joining to localized_messages: use the row whose locale matches the user's locale when available; otherwise fall back to the 'en-US' locale for that template.
  5. Resolve conflicts: if a user has multiple notifications that would be sent at the exact same local_send_time, only keep the notification with the smallest priority value (highest priority). Discard the others.
  6. Filter out malformed templates where base_time_utc IS NULL.

Return one row per final notification to be sent, with the following columns in the result:

  • user_id
  • user_name
  • delivery_channel
  • local_send_time (TIMESTAMP in the user’s local time)
  • locale (the user’s locale)
  • template_id
  • notification_text

Produce the SQL query to generate this notification sheet for 2025-06-01.

Table users

columntypeconstraints
user_idINTPRIMARY KEY, NOT NULL
user_nameVARCHAR(50)NOT NULL
timezone_offset_minutesINTNOT NULL
localeVARCHAR(10)NOT NULL
allow_emailBOOLEANNOT NULL
allow_pushBOOLEANNOT NULL

Sample data:

allow_emailallow_pushlocaletimezone_offset_minutesuser_iduser_name
truetrueen-US-2401Alice
truefalsede-DE602Bob
falsefalseen-IN3303Carol
truetruefr-FR04Dan

Table schedule_templates

columntypeconstraints
template_idINTPRIMARY KEY, NOT NULL
template_nameVARCHAR(100)NOT NULL
base_time_utcTIMESTAMP
priorityINTNOT NULL
active_flagBOOLEANNOT NULL

Sample data:

active_flagbase_time_utcprioritytemplate_idtemplate_name
true2025-06-01 12:00:001101Daily summary
true2025-06-01 16:00:002102Security digest
true2025-06-01 12:00:003103Marketing promo
trueNULL1104Broken template (missing time)

Table user_schedules

columntypeconstraints
user_idINTNOT NULL
template_idINTNOT NULL
start_dateDATENOT NULL
end_dateDATE

Sample data:

end_datestart_datetemplate_iduser_id
NULL2025-01-011011
2025-12-312025-05-011021
NULL2025-02-011012
2025-06-302025-05-151032
NULL2025-01-011013
NULL2025-01-011014
NULL2025-06-011024
NULL2025-06-011044

Table localized_messages

columntypeconstraints
template_idINTNOT NULL
localeVARCHAR(10)NOT NULL
message_textVARCHAR(255)NOT NULL

Sample data:

localemessage_texttemplate_id
en-USGood morning, here is your summary.101
de-DEGuten Morgen, hier ist Ihre Übersicht.101
fr-FRBonjour, voici votre récapitulatif.101
en-USYour security alerts for today.102
fr-FRVos alertes de sécurité du jour.102
en-USDaily special promotion.103

Expected output

Notification sheet for delivery date 2025-06-01 after applying user preferences, local time conversion, localization with fallback to en-US, conflict resolution by priority, and filtering malformed templates. Ordered here by user_id and local_send_time for readability.

delivery_channellocal_send_timelocalenotification_texttemplate_iduser_iduser_name
push2025-06-01 08:00:00en-USGood morning, here is your summary.1011Alice
push2025-06-01 12:00:00en-USYour security alerts for today.1021Alice
email2025-06-01 13:00:00de-DEGuten Morgen, hier ist Ihre Übersicht.1012Bob
push2025-06-01 12:00:00fr-FRBonjour, voici votre récapitulatif.1014Dan
push2025-06-01 16:00:00fr-FRVos alertes de sécurité du jour.1024Dan

Hints

  1. First filter to schedules active on 2025-06-01 and users who allow at least one delivery channel before doing any complex logic.
  2. Use timezone_offset_minutes to shift base_time_utc into local time, COALESCE to implement locale fallback from user locale to en-US, and a ROW_NUMBER window function to drop lower-priority notifications that collide at the same local_send_time.