Reported June 2025
Fivetrangreedy

Aladdin and the Magic Carpet

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

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

Fivetran reported this one in June 2025, and if your OA invite is sitting in your inbox, here's the good news: Aladdin and the Magic Carpet is just the classic gas station circuit in a costume. Magic is gas, dist is the cost to the next stop, and you want the smallest index where a full loop never dips below zero. There's a linear answer and a brute force that will time out at 100000 elements. StealthCoder runs invisibly during the live assessment as a safety net if you blank on the trick, but you should know the shape of this before you open it.

The problem

Aladdin wants to travel once around a circular route containing n magic sources numbered from 0 to n - 1.
You are given two integer arrays of equal length:
magic[i] is the amount of magic collected at source i.
dist[i] is the amount of magic required to travel from source i to source (i + 1) % n.
Aladdin may begin at any source with zero magic. At each source, he collects its magic before paying the travel cost to the next source. His remaining magic may never become negative.
Return the smallest zero-based starting index from which Aladdin can complete exactly one full circuit. If no starting index works, return -1.

Function
optimalPoint(magic: int[], dist: int[]) → int

Examples
Example 1
magic = [1,5,3,2]
dist = [2,2,4,2]
return = 1
Starting at source 0 fails immediately because collecting 1 magic cannot pay a cost of 2.
Starting at source 1, the remaining magic after each trip is 3, 2, 2, and 1. The full circuit succeeds, so the smallest feasible index is 1.
Example 2
magic = [2,1,1]
dist = [3,2,2]
return = -1
The route provides 4 units of magic but requires 7 units in total. No starting point can complete the circuit.
Example 3
magic = [2,2]
dist = [1,1]
return = 0
Either source can begin a successful circuit. The required result is the smaller feasible index, 0.

Constraints
1 <= magic.length == dist.length <= 100000.
0 <= magic[i] <= 10000.
0 <= dist[i] <= 10000.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The problem reduces to one pass with two running numbers. First, if sum(magic) is less than sum(dist), return -1, since no start can work. Otherwise a valid start exists. Walk the array keeping a running tank of magic[i] - dist[i]. When the tank goes negative, every start from the previous candidate up to i is dead, so reset the tank to 0 and set the candidate to i + 1. The final candidate is the answer. The smallest-index requirement is handled for free, because you only move the candidate forward when forced. The common pitfall is simulating each start around the circle, which is O(n^2) and dies at n = 100000. Another trap is forgetting that magic is collected before paying the cost, so you check magic[i] - dist[i] at each step, not after. If the greedy reset logic slips your mind mid-OA, StealthCoder is the hedge that surfaces it.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Aladdin and the Magic Carpet 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as gas station. If you have time before the OA, drill that.

⏵ The honest play

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

Fivetran reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Aladdin and the Magic Carpet FAQ

What's the trick in Aladdin and the Magic Carpet?+

It's the gas station problem. Check total magic against total distance first. If total is short, return -1. Otherwise scan once, track the running surplus, and whenever it goes negative, restart from the next index. The last restart point is your answer.

Why does the greedy reset work?+

If you start at s and first go negative at i, any start between s and i would have arrived at those sources with less or equal magic than you did, since you carried non-negative surplus. So they fail too. Skip all of them and try i + 1.

Will brute force pass?+

No. Trying every start and simulating the loop is O(n^2). With n up to 100000, that's around 10 billion steps in the worst case. You need the single pass O(n) approach with O(1) extra space.

How do I handle the smallest index requirement?+

The greedy pass naturally returns the smallest valid start. You only advance the candidate when the current one provably fails, so the first index that survives the scan with total magic covering total cost is the minimum. No extra tie-breaking needed.

How do I prepare for this in 48 hours?+

Solve the gas station problem from scratch twice, without peeking. Then test edge cases: n = 1, zeros in dist, equal totals, and the example where the answer is 0 with two valid starts. That covers what Fivetran's version tests.

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

OA at Fivetran?
Invisible during screen share
Get it