FastPrepMinimize Adjacent Squared-Difference Cost with One Insertion
Problems/Minimize Adjacent Squared-Difference Cost with One Insertion

Minimize Adjacent Squared-Difference Cost with One Insertion

Microsoft logoMicrosoft● MediumINTERNOA

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a Microsoft intern online assessment. The report gives the main task and the interview setting. We rebuilt the function signature, the output order, the examples and the limits to stay close to it. Our version is about 90% the same as the reported question.

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[]) → long

Examples

Example 1

values = [1,5]return = 8

Inserting 3 changes the cost from 16 to 8.

Example 2

values = [1,2,3]return = 2

Every adjacent gap is one, so insertion cannot reduce the cost.

Example 3

values = []return = 0

Inserting 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 SWE Intern Interviews: 244 Experiences (Part 1 of 6)

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)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public long minimumSquaredDifferenceCost(int[] values) {
        // write your code here
    }
}
Checking account…