Magicsheet logo

Cutting Ribbons

Medium
25%
Updated 8/1/2025

Asked by 2 Companies

Cutting Ribbons

What is this problem about?

The Cutting Ribbons interview question is an optimization problem. You are given an array of ribbon lengths and an integer k. You can cut the ribbons into any number of smaller pieces, but you want to find the maximum integer length L such that you can obtain at least k pieces of length L. This Cutting Ribbons coding problem is a classic "binary search on the answer" task.

Why is this asked in interviews?

Google and Meta ask this to see if a candidate can recognize a monotonic property. If you can cut k ribbons of length 10, you can certainly cut k ribbons of length 5. This realization allows you to avoid trying every possible length and instead use a much faster search strategy.

Algorithmic pattern used

This utilizes the Array, Binary Search interview pattern.

  1. Search Range: The minimum possible length is 1 (or 0 if not possible) and the maximum is the maximum value in the array.
  2. Binary Search: Pick a middle length mid.
  3. Check Function: Count how many pieces of length mid you can get from all ribbons (sum(length / mid)).
  4. Adjust: If count >= k, then mid might be the answer, try a larger length. If count < k, mid is too large, try smaller.

Example explanation

Ribbons: [9, 7, 5], k = 3.

  • Try length 5: 9/5=1, 7/5=1, 5/5=1. Total 3. (Works!)
  • Try length 6: 9/6=1, 7/6=1, 5/6=0. Total 2. (Too few). The maximum length is 5.

Common mistakes candidates make

  • Linear Search: Trying every length from 1 up to the maximum, which is O(Max * N) and very slow.
  • Handling Zero: Failing to return 0 if it's impossible to get k pieces even with length 1.
  • Integer Overflow: Calculating the sum of pieces for very large k or very large ribbon lengths.

Interview preparation tip

Whenever you are asked to find the "maximum minimum" or "minimum maximum" of something, or the "largest possible X that satisfies a condition," immediately check if Binary Search on the Answer is applicable. It’s a very high-signal pattern for interviewers.

Similar Questions