FastPrepOptimize Context Switching
Problems/Optimize Context Switching

Optimize Context Switching

LinkedIn logoLinkedIn● HardOA

Updated Sep 2026

QuestionFunctionExamplesConstraints

Problem statement

A team must optimize context switching between processes. There are n segments where a thread is assigned to each segment. The maximum stack size of a thread assigned in the ith segment is denoted by threadSize[i], for all 1 ≤ i ≤ n. For any high-priority process, some stack size of the segments must be increased. A segment is said to be special when threadSize[i - 1] < threadSize[i] > threadSize[i + 1]. The segments at each end cannot be special. The stack sizes must be modified in such a way as to maximize the number of special segments. To do so, choose any segment and increase the stack size by x. More formally,

  1. Choose an index i and an integer x, where 0 ≤ x ≤ 10^18.
  2. Increase the stack size of the ith segment from threadSize[i] to threadSize[i] + x.

Find the minimum total increase in the stack size of the segments.

Function

optimizeContextSwitching(threadSize: int[]) → long

Examples

Example 1

threadSize = [3, 1, 4, 5, 5, 2]return = 3
Example 1 illustration

It is optimal to add 2 and 1 to the third and fifth segments, respectively. The final stack sizes of the segments are [3,1,6,5,6,2].

So the answer = 2 + 1 = 3.

It can be seen that with this change, the required configuration is achieved with a maximum possible of 2 special segments. This is the minimum possible total increase in the stack sizes.

Example 2

threadSize = [3, 2, 1, 2, 3]return = 4

Since, the segments at the end cannot be special, both the 2nd and 4th segments must be increased by 2. The final stack sizes are [3, 4, 1, 4, 3].

Constraints

  • 🍉🍉

More LinkedIn interview questions

  • Allocate Mailboxes with Squared DistanceONSITE INTERVIEW · Seen Nov 2026
  • Find Leaves of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
  • Max Points on a LineONSITE INTERVIEW · Seen Nov 2026
  • Maximum Product of a Contiguous SubarrayPHONE SCREEN · Seen Nov 2026
  • Maximum Sum of a Contiguous SubarrayPHONE SCREEN · Seen Nov 2026
  • Merge Two Sorted ListsPHONE SCREEN · Seen Nov 2026
  • Sqrt(x)PHONE SCREEN · Seen Nov 2026
  • Total Years of Work ExperiencePHONE SCREEN · Seen Nov 2026
  • Explore an Unknown Grid and Find a PathONSITE INTERVIEW · Seen Nov 2026
  • Fire Station Response BillONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Hard Disk Clustering CostConfluent · OAConfluent interview questions
  • Minimum BeautyBloomberg LP · OABloomberg LP interview questions
  • Minimum Operations to Make a SubsequenceGoogle · OAGoogle interview questions
  • Maximize K-Element AND with an Increment BudgetAmazon · OA
  • Split Array to Minimize the Largest SumInfosys · OAInfosys interview questions
  • Split Array Largest SumAuxoAI · OA
  • Delete and Freeze GameRubrik · OARubrik interview questions
  • Lock CodeVisa · OAVisa interview questions
  • Starlink Beam PlannerSpaceX · OA · PHONE SCREENSpaceX interview questions
  • Most Segments That Sort the ArrayRippling · OARippling interview questions

LinkedIn interview experiences

  • LinkedIn Senior MLE Interview Questions: 1 Experience
  • LinkedIn Staff AI Engineer Phone Screens: 1 Experience
  • LinkedIn Experienced MLE Onsite: 1 Experience
  • LinkedIn Coding with AI Interview: 4 Experiences
  • LinkedIn Experienced Infrastructure SWE Onsite: 1 Experience
All LinkedIn interview questionsSee LinkedIn hiring insights
Saved
class Solution {
    public long optimizeContextSwitching(int[] threadSize) {
      // write your code here
    }

}
Checking account…