Minimize Adjacent Squared-Difference Cost with One Insertion
Updated
Problem statement
The cost of an integer array is the sum of (values[i]-values[i-1])^2 for every adjacent pair.
Insert exactly one integer anywhere, including either end, and return the minimum possible cost. The inserted integer may be any signed 32-bit value.
Function
minimumSquaredDifferenceCost(values: int[]) → longExamples
Example 1
values = [1,5]return = 8Inserting 3 changes the cost from 16 to 8.
Example 2
values = [1,2,3]return = 2Every adjacent gap is one, so insertion cannot reduce the cost.
Example 3
values = []return = 0Inserting into an empty array leaves no adjacent pair.
Constraints
0 <= values.length <= 100000.-10^9 <= values[i] <= 10^9.- The answer fits in a signed 64-bit integer.
More Microsoft Online Assessment questions
- Minimum Cost to Process Priority TasksOA · Seen Nov 2026
- Most Visited Sprint MarkerOA · Seen Nov 2026
- Maximum Escape Game ScoreOA · Seen Nov 2026
- Count Subarrays with a Perfect-Square ProductOA · Seen Nov 2026
- Maximum Interactive Team SizeOA · Seen Nov 2026
- Maximize Pipeline Throughput Under a Scaling BudgetOA · Seen Nov 2026
- Minimum Cost K-Capable ModelsOA · Seen Nov 2026
- Find Available Meeting RoomsOA · Seen Oct 2026
- Maximum Sum of a K-StarOA · Seen Oct 2026
- Capacity to Ship Packages Within DaysOA · ONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Minimize Adjacent Squared-Difference Cost with One InsertionMcKinsey & Company · OAMcKinsey & Company interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Minimum Values with Even Powers of TwoVisa · OAVisa interview questions
- Minimum Halvings for Alternating ParityIBM · OAIBM interview questions
- Resource AllocationMaven Securities · OAMaven Securities interview questions
- Rock JumpingIMC Trading · OAIMC Trading interview questions
- Minimum Total Batch ExpenseAmazon · OA
- Minimum Increments for Stepwise TowersZipRecruiter · OAZipRecruiter interview questions
- Server Investment 🐳TikTok · OATikTok interview questions
Interview reports that mention this question
Microsoft interview experiences
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)
- Microsoft SWE Intern, Early 2026: 54 Experiences (Part 2 of 6)