Connect N Ropes With Minimum Cost
Updated
Problem statement
You are given an integer array ropes, where ropes[i] is the length of one rope.
In one operation, choose any two ropes, connect them into one rope, and pay a cost equal to the sum of their lengths. The new rope has that same combined length.
Return the minimum total cost required to connect all ropes into one rope. If there is only one rope, return 0.
Function
minimumRopeConnectionCost(ropes: int[]) → longExamples
Example 1
ropes = [4,3,2,6]return = 29Connect lengths 2 and 3 for cost 5, then 4 and 5 for cost 9, and finally 6 and 9 for cost 15. The total is 5 + 9 + 15 = 29.
Example 2
ropes = [4,2,7,6,9]return = 62One optimal sequence has merge costs 6, 12, 16, and 28, for a total of 62.
Example 3
ropes = [10]return = 0Only one rope is present, so no connection operation is needed.
Constraints
1 <= ropes.length <= 10^51 <= ropes[i] <= 10^4- The result fits in a signed
64-bit integer.
Source note: Author-written prompt excerpt retained for source fidelity.
More Amazon Onsite Interview questions
- Next Greater Element IONSITE INTERVIEW · Seen Dec 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Merge IntervalsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Optimal Account BalancingONSITE INTERVIEW · Seen Dec 2026
- Task SchedulerOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Dec 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Flood FillONSITE INTERVIEW · Seen Nov 2026
- Merge k Sorted ListsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
- API Call Thread Pool ScheduleBaseten · ONSITE INTERVIEWBaseten interview questions
- Minimum Workers with Job AssignmentsLyft · ONSITE INTERVIEWLyft interview questions
- Ad Rotation Scheduler With CooldownGoogle · ONSITE INTERVIEWGoogle interview questions
- Most Transactions Kept Without Going NegativeDocuSign · ONSITE INTERVIEWDocuSign interview questions
- Maximum Supplier ProfitExpedia · ONSITE INTERVIEWExpedia interview questions
- Minimum Stick Connection CostNavan · ONSITE INTERVIEWNavan interview questions
- Movie Schedule for Maximum EnjoymentC3.ai · ONSITE INTERVIEWC3.ai interview questions
- Allocate Customers to Preferred SitesYelp · ONSITE INTERVIEWYelp interview questions
- Greedy and Beam Sentence DecodingMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions