Reported March 2019
Airbnbshortest path

Minimum Wizard Referral Cost

Reported by candidates from Airbnb's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Airbnb OA. Under 2s to a working solution.
Founder's read

The whole problem hinges on a priority queue. Airbnb reported this one in March 2019, and if your OA invite is a day or two out, know it now: it's a weighted directed graph dressed up as wizards and referrals. Edges out of wizard 0 cost nothing, every other edge costs (j - i) squared, and you want the cheapest way to reach wizard n - 1. Cycles are allowed, so a naive recursion will hang. If you blank on the setup during the live OA, StealthCoder runs invisibly as a safety net and hands you the approach.

The problem

There are n wizards ranked from 0 through n - 1. The string referrals[i] contains the space-separated ranks of the wizards whom wizard i can introduce. An empty string means that wizard i has no referrals.
You are wizard 0. You already know every wizard listed by wizard 0, so following an edge from wizard 0 costs 0. For every other directed referral from wizard i to wizard j, the introduction costs (j - i)2.
Return the minimum total cost needed to meet the highest-ranked wizard, n - 1. Return -1 if that wizard cannot be reached. The referral graph may contain cycles.

Function
minimumWizardReferralCost(referrals: String[]) → int

Examples
Example 1
referrals = ["1 2","3","3",""]
return = 1
Wizards 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 = 0
Wizard 0 already knows the highest-ranked wizard directly, so no paid introduction is needed.
Example 3
referrals = ["1","0",""]
return = -1
The cycle between wizards 0 and 1 never reaches wizard 2.

Constraints
2 <= referrals.length <= 500.
Each non-empty row contains distinct integer ranks separated by one space.
Every listed rank is in [0, referrals.length - 1].
The graph may contain directed cycles.

Reported by candidates. Source: FastPrep

Pattern and pitfall

This is single-source shortest path with non-negative weights, so Dijkstra with a min-heap is the tool. Parse each referrals[i] by splitting on spaces, and skip empty strings so you don't crash on int(''). Edges leaving node 0 get weight 0. Every other edge gets (j - i)^2. Push (0, 0) into the heap, pop the cheapest, skip stale entries, relax neighbors, and return the distance to n - 1, or -1 if it's never reached. The common pitfall is applying the zero cost to edges that merely lead to wizard 0's neighbors instead of only edges leaving wizard 0. Example 1 shows this: going 2 to 3 costs 1, not 0. Another trap is the cycle in Example 3, which a visited check or distance comparison handles. With n up to 500, performance is fine. StealthCoder is your hedge if the heap code slips on the day.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Minimum Wizard Referral Cost cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. If you're reading this with an OA window open, you're who this was built for.

Get StealthCoder

Related leaked OAs

⏵ The honest play

You've seen the question. Make sure you actually pass Airbnb's OA.

Airbnb reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Wizard Referral Cost FAQ

What's the trick in Minimum Wizard Referral Cost?+

Treat it as a weighted directed graph and run Dijkstra. Edges out of wizard 0 cost 0, all other edges cost (j - i) squared. A min-heap gives you the cheapest path to wizard n - 1, and the same pass handles cycles safely.

Why can't I use plain BFS here?+

BFS counts edges, not cost. In Example 1 the route through wizard 2 costs 1 while the route through wizard 1 costs 4, though both have the same hop count. Weights differ per edge, so you need Dijkstra, not an unweighted traversal.

How do I handle empty referral strings?+

Splitting an empty string on a space gives a list with one empty item in some languages, which breaks integer parsing. Check that the string is non-empty, or filter out blank tokens, before converting to ints and adding edges.

When do I return -1?+

Return -1 if the heap empties and wizard n - 1 never got a finite distance. Example 3 shows it: wizards 0 and 1 point at each other, and wizard 2 is unreachable. Keep distances initialized to infinity and check at the end.

How should I prepare in 48 hours for this kind of OA?+

Write Dijkstra from memory twice with a heap, including the stale-entry skip. Then practice parsing odd input formats like space-separated strings inside an array. Those two things cover almost everything this Airbnb problem, reported March 2019, tests.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Airbnb.

OA at Airbnb?
Invisible during screen share
Get it