FastPrepAladdin and the Magic Carpet
Problems/Aladdin and the Magic Carpet

Aladdin and the Magic Carpet

Fivetran logoFivetran● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report does not give the exact function signature, the tie rule, or the number limits. We chose those. Our version is about 96% the same as the reported question.

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.

More Fivetran interview questions

  • Execute In-Memory SQL Storage OperationsPHONE SCREEN · Seen Sep 2026
  • Minimum Fuel Car AlignmentPHONE SCREEN · Seen Jun 2023
  • Reschedule Meetings for Maximum Free TimeOA · Seen Nov 2026
  • Connected GroupsONSITE INTERVIEW · Seen Jun 2024
  • Unique Near-Equal Target-Sum PairsONSITE INTERVIEW · Seen Jun 2024

Similar questions at other companies

  • Fair Capped Money DistributionGusto · PHONE SCREEN · ONSITE INTERVIEWGusto interview questions
  • Largest Number Below N from Reusable DigitsByteDance · PHONE SCREENByteDance interview questions
  • Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
  • Jump GamePaytm · PHONE SCREENPaytm interview questions
  • Remove the Fewest Magic BeansThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
  • Gas Station CircuitAngel One · PHONE SCREEN
  • Aggressive CowsAmazon · PHONE SCREEN
  • Minimum Cost for a Target WeightPace Stock Broking Services · PHONE SCREEN
  • Defer Expenses to Preserve BalanceDRW · PHONE SCREENDRW interview questions
  • Container With Most WaterEPAM · PHONE SCREENEPAM interview questions

Interview reports that mention this question

  • Fivetran SDE 2 OA and Technical Round: 1 Experience

Fivetran interview experiences

  • Fivetran Staff SRE Hiring Manager Interview: 1 Experience
  • Fivetran Software Engineer HackerRank and Java: 1 Experience
  • Fivetran Senior SRE Interview Process: 1 Experience
  • Fivetran Software Engineer HashMap and Design: 1 Experience
  • Fivetran SDE-2 Coding and Parking Lot Design: 1 Experience
All Fivetran interview questionsSee Fivetran hiring insights
Saved
class Solution {
    public int optimalPoint(int[] magic, int[] dist) {
        // Write your code here.
    }
}
Checking account…