Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Get started
Randomly Group Passenger IDs
00:00
5 left

Randomly Group Passenger IDs

MediumSQL · PostgreSQL

Problem

American Airlines wants to run a randomized experiment using eligible AAdvantage passengers from recent flight bookings. Write a PostgreSQL query that assigns each eligible passenger ID to one of three mutually exclusive groups: 10% treatment, 10% holdout, and 80% control.

Requirements

  1. Include only passengers with a confirmed booking between 2026-07-01 and 2026-07-31.
  2. Remove duplicate passengers who have multiple qualifying bookings.
  3. Randomize the eligible passengers, then assign the first 10% to treatment_10_pct, the next 10% to holdout_10_pct, and the remainder to control_80_pct.
  4. Return each passenger ID, assigned group, and position within that group. For sample data with 10 eligible passengers, the groups should contain 1, 1, and 8 passengers.

Output details

  • Return columns in this order: passenger_id, group_name, position_in_group.
  • Order results by groups as treatment_10_pct, holdout_10_pct, then control_80_pct, with positions ascending within each group.
  • Order passengers deterministically by ascending md5(passenger_id::text).
  • Use ceiling thresholds: treatment through CEIL(eligible_count * 0.10) and holdout through CEIL(eligible_count * 0.20).
  • Interpret the date range as inclusive of 2026-07-01 and exclusive of 2026-08-01.

Schema

passengers
ColumnTypeDescription
passenger_idPKINTEGERUnique passenger identifier
passenger_nameVARCHAR(100)Passenger name
flight_bookings
ColumnTypeDescription
booking_idPKINTEGERUnique booking identifier
passenger_idINTEGERPassenger associated with the booking
flight_numberVARCHAR(10)American Airlines flight number
flight_dateDATEScheduled flight date
booking_statusVARCHAR(20)Booking status
Tablespassengersflight_bookings
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements and the two tables are in the Question tab.

You need to log in / sign up to run or submit.
CodePostgreSQL
Sign up free to run your codeLog inLn 1
Run your query to see results here.