Minimum Wizard Referral Cost
Updated
Problem statement
There are n wizards, ranked from 0 through n - 1. The string referrals[i] holds the ranks of the wizards whom wizard i can introduce, separated by spaces. An empty string means wizard i has no referrals.
You are wizard 0. You already know every wizard listed by wizard 0, so going from wizard 0 to one of them costs 0. Any other referral goes one way, from wizard i to a wizard j on i's list. That introduction costs (j - i)2.
Return the lowest total cost to meet the highest-ranked wizard, n - 1. Return -1 if you cannot reach that wizard. The referrals may form cycles, where following them leads back to a wizard you already met.
Function
minimumWizardReferralCost(referrals: String[]) → intExamples
Example 1
referrals = ["1 2","3","3",""]return = 1Wizards 1 and 2 are both known for free. Asking wizard 2 to introduce wizard 3 costs (3 - 2)^2 = 1, which is cheaper than the cost 4 route through wizard 1.
Example 2
referrals = ["3","","",""]return = 0Wizard 0 already knows the highest-ranked wizard directly, so no paid introduction is needed.
Example 3
referrals = ["1","0",""]return = -1The cycle between wizards 0 and 1 never reaches wizard 2.
Constraints
2 <= referrals.length <= 500.- Each non-empty string holds distinct integer ranks, separated by one space.
- Every listed rank is in
[0, referrals.length - 1]. - The referrals may form cycles.
More Airbnb Online Assessment questions
- In-Memory File SystemOA · Seen Nov 2026
- Task Management SystemOA · Seen Nov 2026
- Recipe Management SystemOA · Seen Nov 2026
- Progressive Banking SystemOA · Seen Nov 2026
- Credit Accounts with Scheduled TransfersOA · Seen Oct 2026
- Parcel Attribute Store with TTLOA · Seen Oct 2026
- Parcel Event TrackingOA · Seen Oct 2026
- Median Home Price by CityOA · Seen Sep 2026
- Worker Management (Promotion Track), Part 4: Full-Shift Double PayOA · Seen Sep 2026
- Worker Management (Promotion Track), Part 3: Promotions and SalaryOA · Seen Sep 2026
Similar questions at other companies
- Minimum Stress PathWalmart · OAWalmart interview questions
- Dijkstra Shortest Paths in a Weighted Undirected GraphVisa · OAVisa interview questions
- Minimize Path ValuePalantir · OAPalantir interview questions
- Minimize Path ValueSalesforce · OASalesforce interview questions
- Cookie Monster Minimum Arrival TimeIntuit · OAIntuit interview questions
- Minimum Travel Time with Flashing Traffic LightsPeopleStrong · OAPeopleStrong interview questions
- Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
- Cheapest Flights Within K StopsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Shortest Paths in a Nonnegative Directed GraphNation with NaMo · PHONE SCREEN
- Minimum Product Path in a Directed GraphGoogle · ONSITE INTERVIEWGoogle interview questions
Airbnb interview experiences
- Airbnb OA · November 2026
- Airbnb Campus and Full-Time SWE OA / Interviews: 9 Experiences
- Airbnb Intern Phone Interviews: 21 Experiences (Part 7 of 7)
- Airbnb Intern OA and Phone Screens: 17 Experiences (Part 6 of 7)
- Airbnb Intern OA and Onsite Rounds: 16 Experiences (Part 5 of 7)
- Airbnb Intern OA, Diplomacy Game: 19 Experiences (Part 4 of 7)