Cut Ribbons to Maximize Equal Length
Updated
Problem statement
You are given an array ribbons, where ribbons[i] is the positive integer length of one ribbon, and a positive integer k.
You may cut each ribbon into any number of integer-length pieces. Pieces from different ribbons cannot be joined. Return the greatest integer length length for which the ribbons can produce at least k pieces of exactly that length.
The input guarantees that at least k pieces of length 1 can be produced.
Follow-up
How would the stopping condition and return value change if ribbon lengths and the requested answer could be fractional?
Function
maxLength(ribbons: int[], k: int) → intExamples
Example 1
ribbons = [9,7,5]k = 3return = 5Lengths 5, 5, and 5 can be cut from the three ribbons. Length 6 yields only two pieces.
Example 2
ribbons = [7,5,9]k = 4return = 4The ribbons produce 1 + 1 + 2 = 4 pieces of length 4. A length of 5 produces only three pieces.
Example 3
ribbons = [4,4,4]k = 6return = 2Each ribbon contributes two pieces of length 2.
Constraints
ribbonsis non-empty.- Every ribbon length is a positive integer.
kis positive and does not exceed the total number of unit-length pieces.- The total number of pieces is accumulated with a signed
64-bit integer.
More Google Onsite Interview questions
- Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
- Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
- Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
- Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
- Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
- Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Search in a Rotated Sorted ArrayAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Search a Row- and Column-Sorted MatrixByteDance · ONSITE INTERVIEWByteDance interview questions
- 3Sum SmallerSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Smallest Batch Size Within a Step LimitMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Peak of an Increasing-Then-Decreasing ArraySnap Inc. · ONSITE INTERVIEWSnap Inc. interview questions
- Online Meeting Scheduler with Fixed RoomsUber · ONSITE INTERVIEWUber interview questions
- Search a 2D MatrixWhatNot · PHONE SCREEN · ONSITE INTERVIEWWhatNot interview questions
- Search in Rotated Sorted ArrayKotak Mahindra Bank · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Find First and Last Position in Sorted ArrayPaytm · ONSITE INTERVIEWPaytm interview questions
- Detect a Silent SensorConfluent · ONSITE INTERVIEWConfluent interview questions