KatInterview

Time 45 Minutes Using Two Non-Uniform Burning Cords

Math & Bit ManipulationAuto-graded · 17 test cases
MediumAuto-gradedMath & Bit Manipulationlogicreasoningmath

Last updated

Tests logical reasoning about burn-time budgets and how lighting both ends of a cord precisely halves its remaining duration regardless of density.

Problem

You have two cords and a box of matches. Each cord takes exactly 60 minutes to burn from one end to the other. The material is unevenly packed, so burning speed varies along the length — half the physical length of a cord does not represent half the burn time.

You may ignite either end of either cord at any moment. Your task is to produce a procedure that times exactly 45 minutes, starting from your first match strike.

Key Facts

  • Igniting one end of a cord lets it burn for 60 minutes total.
  • Igniting both ends of a cord simultaneously causes the two flames to consume the cord's total remaining "fuel" at double speed. The cord is gone in exactly half of whatever one-end time it had left, regardless of where the flames happen to meet — because only total fuel matters, not position.
  • You cannot cut the cords, fold them, or use any other measuring device.

What to Return

Return the exact six-step sequence below (as a List[str]), in order, with no modifications:

"At time 0, light both ends of rope A."
"At time 0, light one end of rope B."
"Rope A finishes at time 30 minutes, because it is burning from both ends."
"At time 30, light the other end of rope B."
"Rope B has 30 minutes of one-end burning remaining, which takes 15 minutes when lit from both ends."
"Rope B finishes at time 45 minutes."

The judge compares your return value against this list exactly.

Constraints

  • The function takes no parameters and returns a List[str] of exactly 6 elements.
  • Each string must match the expected output character-for-character.
  • No other measuring tools, no cutting, no folding.
  • The procedure must work regardless of how burn density is distributed along either cord.

Examples

Example 1

Input: measure_45_minutes()

Output: ['At time 0, light both ends of rope A.', 'At time 0, light one end of rope B.', 'Rope A finishes at time 30 minutes, because it is burning from both ends.', 'At time 30, light the other end of rope B.', 'Rope B has 30 minutes of one-end burning remaining, which takes 15 minutes when lit from both ends.', 'Rope B finishes at time 45 minutes.']

Example 2

Input: measure_45_minutes()

Output: ['At time 0, light both ends of rope A.', 'At time 0, light one end of rope B.', 'Rope A finishes at time 30 minutes, because it is burning from both ends.', 'At time 30, light the other end of rope B.', 'Rope B has 30 minutes of one-end burning remaining, which takes 15 minutes when lit from both ends.', 'Rope B finishes at time 45 minutes.']

Hints

Hint 1

Think of each cord as holding a fixed 'budget' of burn-time fuel rather than a physical length — what operation halves that budget instantly?

Hint 2

Cord A burning from both ends disappears in 30 minutes and acts as a precise timer; what does that tell you about how much fuel cord B has left at that moment?

Hint 3

The moment cord A burns out is your signal to change something about cord B — what one action at t = 30 converts a 30-minute remainder into a 15-minute one?