Associate Intern Interview Questions

1,283 associate intern interview questions shared by candidates

Given an array of n positive integers, assuming 0-based indexing, its cost is the sum from i=1 to i=(len(arr) - 1) of (arr[i] - arr[i-1])^2. We want to find the minimum possible cost of the array after inserting exactly one element. For example, the cost of the array a = [1, 3, 5, 2, 10] before insertion is (1 - 3)^2 + (3 - 5)^2 + (5 - 2)^2 + (2 - 10)^2 = 81. After inserting 6 between 2 and 10, the cost of the array a is (1 - 3)^2 + (3 - 5)^2 + (5 - 2)^2 + (2 - 6)^2 + (6 - 10)^2 = 49. It can be proven that 49 is the minimum possible cost for the array a. Complete the function public static long getMinimumCost(List arr) to solve this problem.
avatar

Associate Developer Intern

Interviewed at IBM

3.9
Oct 6, 2023

Given an array of n positive integers, assuming 0-based indexing, its cost is the sum from i=1 to i=(len(arr) - 1) of (arr[i] - arr[i-1])^2. We want to find the minimum possible cost of the array after inserting exactly one element. For example, the cost of the array a = [1, 3, 5, 2, 10] before insertion is (1 - 3)^2 + (3 - 5)^2 + (5 - 2)^2 + (2 - 10)^2 = 81. After inserting 6 between 2 and 10, the cost of the array a is (1 - 3)^2 + (3 - 5)^2 + (5 - 2)^2 + (2 - 6)^2 + (6 - 10)^2 = 49. It can be proven that 49 is the minimum possible cost for the array a. Complete the function public static long getMinimumCost(List arr) to solve this problem.

Viewing 921 - 930 interview questions

See Interview Questions for Similar Jobs

Glassdoor has 1,283 interview questions and reports from Associate intern interviews. Prepare for your interview. Get hired. Love your job.