cswork / libraryOpen practice workspace

Coding questions · Bloomberg

Find maximum cable length by cutting

This question evaluates a candidate's proficiency in algorithm design, particularly applying binary search over a monotonic answer space and managing integer versus floating-point precision when maximizing piece length.

You are given N cables with integer lengths and a target K. Cut the cables into pieces of equal integer length L to produce at least K pieces; maximize L. If impossible, return 0. Describe an algorithm using binary search on the answer and analyze its complexity. Follow up: support non-integer lengths with precision 1e-3.

Maximum Cable Cut Length

Given cable lengths and target piece count k, return the maximum integer piece length that can produce at least k pieces.

Examples

Example 1

Input: ([802, 743, 457, 539], 11)
Output: 200

Explanation: Classic cable cutting.

Example 2

Input: ([5, 5], 20)
Output: 0

Explanation: Impossible target.

Hints

  1. Choose a representation that makes the core operation simple.
  2. Handle empty and boundary inputs before the main algorithm.