Magicsheet logo

Smallest Range I

Easy
62.5%
Updated 8/1/2025

Asked by 2 Companies

Topics

Smallest Range I

What is this problem about?

The "Smallest Range I" interview question is an introductory problem about range optimization. You are given an array nums and an integer k. For each element, you can add any value between -k and k to it. After these operations, you want to find the minimum possible difference between the maximum and minimum values of the modified array. This "Smallest Range I coding problem" is about compressing the array's range as much as possible.

Why is this asked in interviews?

Companies like Adobe and Glassdoor use this to test basic mathematical intuition. It's an "EASY" problem that asks if you can see the forest for the trees—you don't need to decide the values for every element; you only need to focus on the extreme values (the global min and global max).

Algorithmic pattern used

This follows the "Array and Math interview pattern". The original range is max(nums) - min(nums). You can increase the minimum by at most k and decrease the maximum by at most k.

  1. Potential New Min: min(nums) + k.
  2. Potential New Max: max(nums) - k.
  3. The new difference is (max - k) - (min + k) = (max - min) - 2k.
  4. If this difference is negative, it means the numbers can all be made equal, so the result is 0. Result: max(0, (max(nums) - min(nums)) - 2*k).

Example explanation

Array: [1, 10], k = 3.

  1. Original min = 1, max = 10.
  2. Max possible min = 1 + 3 = 4.
  3. Min possible max = 10 - 3 = 7.
  4. Difference: 7 - 4 = 3. Result: 3. If k = 5: max - min = 9. 9 - 2*5 = -1. Result: 0 (we can make both 5.5, but since we use integers, we can make both 6).

Common mistakes candidates make

A common mistake is trying to modify all elements or using a greedy approach on each element one by one. Another mistake is forgetting to handle the case where the difference becomes negative, which should return 0. Some candidates also get confused and try to subtract k from the minimum and add to the maximum, which actually increases the range.

Interview preparation tip

In range problems, the middle elements are usually irrelevant. Focus on the min and max. This "Smallest Range I interview question" is a great example of how mathematical simplification can eliminate the need for complex loops.

Similar Questions