Forty-Five-Minute Two-Cord Timer
Medium · Coding & Algorithms · Software Engineer · Technical Screen
Problem Statement
You are given two non-uniform chemical fuses (A and B) and an unlimited box of matches, with no stopwatch, ruler, or knife.
Each fuse takes an exact total of 60 minutes to burn end-to-end when lit at a single tip. However, burn speed varies unpredictably along each fuse's length—for example, the first 90% of physical length might flash past in 12 minutes while the dense final 10% smolders for 48 minutes—so geometric distance never tells you elapsed time.
Physical rules:
- You may ignite one tip or both tips of a fuse at
t = 0or at the exact moment an active fuse finishes burning out (the only observable time signals available without a clock). - You may ignite the unlit second tip of a fuse that is already burning from its first tip.
- Cutting, folding, or extinguishing a fuse is forbidden.
Implement measure_45_minutes() to return the chronological event log that measures an exact 45-minute span starting at t = 0 and ending at t = 45. Assign Fuse A as the fuse that burns out first and Fuse B as the fuse that completes the 45-minute measurement, formatting each entry with these two exact templates:
- Ignition action:
"t=<minute>: ignite <both ends|one end|other end> of fuse <A|B>." - Burnout milestone:
"t=<minute>: fuse <A|B> burns out."
At any shared timestamp t, list burnout milestones before newly triggered ignitions, and list Fuse A before Fuse B when both are ignited at t = 0.
def measure_45_minutes() -> List[str]:
Examples
Example 1
- Input:
measure_45_minutes() - Output:
python [ "t=0: ignite both ends of fuse A.", "t=0: ignite one end of fuse B.", "t=30: fuse A burns out.", "t=30: ignite other end of fuse B.", "t=45: fuse B burns out.", ] - Explanation: Two flames on Fuse
Aconsume2minutes of fuel per clock minute, meeting att = 30. Meanwhile, one flame on FuseBconsumes30minutes of fuel, leaving30minutes of fuel unburned; igniting FuseB's opposite tip att = 30halves that remainder to15minutes, ending att = 45.
Example 2
- Input: Simulate
measure_45_minutes()when FuseAhas two equal-length halves taking[12, 48]single-flame minutes and FuseBhas three segments taking[5, 15, 40]single-flame minutes. - Output: Fuse
Aburns out at30.0minutes; FuseBburns out at45.0minutes (steps[-1] == "t=45: fuse B burns out."). - Explanation: On Fuse
A, the left flame clears the12-minute half att = 12while the right flame eats12minutes of the48-minute half; both flames then share the remaining36fuel-minutes for36 / 2 = 18minutes (12 + 18 = 30). On FuseB, one flame clears5 + 15 + 10 = 30fuel-minutes byt = 30, and two flames finish the remaining30fuel-minutes in30 / 2 = 15minutes (30 + 15 = 45).
Constraints
- No external timing or length-measuring instruments exist; ignitions may only occur at
t = 0or at a prior fuse burnout timestamp. - Every entry in the returned
List[str]must follow the exact ignition or burnout template with integer minute timestamps.
Step-by-step walkthrough
Understand the puzzle and event-log contract
We hold two chemical fuses (
AandB) and unlimited matches, without a clock, ruler, or blade. Each fuse burns end-to-end in60minutes under a single flame, yet local burn speed varies wildly—for instance, in Example 2 FuseA's left half takes12minutes while its dense right half takes48minutes.measure_45_minutes()must return the chronologicalList[str]of"t=<minute>: ..."entries describing how we measure45exact minutes fromt = 0tot = 45.Count in "fuel-minutes" instead of physical length
Centimeters tell us nothing because thin stretches race and dense knots crawl. Instead, measure each fuse in fuel-minutes: the single-flame burn duration contained inside it (
60fuel-minutes total).- One active flame always consumes
1fuel-minute per clock minute, whether moving fast across thin material or creeping across a dense knot. - Two opposite flames burning simultaneously each consume
1fuel-minute per clock minute, draining1 + 1 = 2fuel-minutes per clock minute combined until they meet!
Example 2's uneven Fuse A ([12, 48] fuel-minutes): |<------ left half: 12 fuel-min ------>|<----- right half: 48 fuel-min ----->| Flame L --> <-- Flame R First 12 min: Flame L finishes the entire left half (12 fuel-min); Flame R consumes 12 fuel-min from the right, leaving 36 fuel-min. Next 18 min: Both flames attack the remaining 36 fuel-min from opposite sides at 2 fuel-min/min -> 36 / 2 = 18 more minutes. Total time: 12 + 18 = 30 minutes!Regardless of internal density, igniting both ends of any fuse segment holding
Tfuel-minutes always finishes inT / 2clock minutes.- One active flame always consumes
Trigger actions only on observable events
With no watch on our wrist, we cannot say "wait
15minutes and strike a match." Only two kinds of moments are directly observable:- The initial start (
t = 0), and - The instant an active fuse completely burns out.
Igniting both ends of Fuse
Aatt = 0creates a guaranteed observable signal att = 30.- The initial start (
Drain Fuse B in parallel to leave 30 fuel-minutes
Because
45 = 30 + 15and15 = 30 / 2, we want FuseBto arrive att = 30with exactly30fuel-minutes left so that igniting its opposite end halves that remainder into15clock minutes.How do we remove
30fuel-minutes from FuseBduring the first30minutes? Ignite one end of FuseBright att = 0alongside FuseA!- Leaving Fuse
Bunlit untilt = 30leaves all60fuel-minutes intact, so FuseBcould only finish at30 + 30 = 60(both ends) or30 + 60 = 90(one end). - Burning one end of Fuse
Bfromt = 0tot = 30consumes30fuel-minutes, leaving60 - 30 = 30fuel-minutes unburned regardless of how uneven FuseBis.
- Leaving Fuse
Trace Example 2 across both fuses
Let's verify the timeline on Example 2 (
Fuse A = [12, 48],Fuse B = [5, 15, 40]):Clock Fuse A ([12, 48] = 60 fuel-min) Fuse B ([5, 15, 40] = 60 fuel-min) -------- --------------------------------- ----------------------------------------- t = 0 Ignite BOTH ends (2 flames) Ignite ONE end (left flame only) t = 0..30 Drains 2 fuel-min/min (60 eaten) Clears 5 + 15 + 10 = 30; 30 fuel-min left t = 30 Flames meet -> FUSE A BURNS OUT! Signal! Ignite OTHER end (2 flames) t = 30..45 (out) Drains 30 fuel-min at 2/min -> 15 min t = 45 (out) Flames meet -> FUSE B BURNS OUT! (45 min)Formatting each milestone with the required templates (
t=30burnout beforet=30ignition, FuseAbefore FuseBatt=0) yields five crisp entries.Complete solution
Return the five-entry chronological log matching the problem templates.
from typing import Any, Dict, List, Optional, Set, Tuple def measure_45_minutes() -> List[str]: # event_log: chronological sequence of observable ignitions and burnouts formatted per specification. # Phase 1 (t = 0): Start a 30-min two-flame timer on Fuse A while draining 30 fuel-minutes on Fuse B. event_log = [ "t=0: ignite both ends of fuse A.", "t=0: ignite one end of fuse B.", # Phase 2 (t = 30): Two flames consume 2 fuel-minutes per clock minute, so Fuse A finishes at 60 / 2 = 30. # Fuse A's burnout is the observable signal that triggers lighting Fuse B's opposite tip without a clock. "t=30: fuse A burns out.", "t=30: ignite other end of fuse B.", # Phase 3 (t = 30 to 45): Fuse B has 60 - 30 = 30 fuel-minutes left; two flames halve that to 15 minutes. "t=45: fuse B burns out.", ] return event_log✅ Submit now — all tests should pass.
Intuition & how it works
Imagine clearing a 60-minute platter of irregular appetizers where some pieces chew slowly and others vanish in seconds: a ruler cannot locate the halfway mark, yet one person eating steadily from the left always clears 1 minute's worth of food per clock minute. Start a second person from the right edge at the exact same moment and together they clear 2 minutes' worth per clock minute, guaranteed to meet and empty the 60-minute platter in 60 / 2 = 30 minutes! This puzzle pairs rate superposition — summing independent flame rates so two opposite flames consume 2 fuel-minutes per real minute no matter where they meet — with event-driven synchronization, striking matches solely at observable milestones (t = 0 or a fuse burnout) rather than guessing elapsed time.
Why do intuitive shortcuts fail? Measuring or folding a fuse to find its geometric midpoint fails because non-uniform density lets one physical half flash past in 12 minutes while the other half takes 48 minutes. Waiting until Fuse A finishes its 30-minute two-ended burn before lighting Fuse B also misses 45: an untouched 60-minute fuse lit at t = 30 can only end at 30 + 30 = 60 minutes (both ends) or 30 + 60 = 90 minutes (one end).
Let's trace Example 2 where Fuse A has segments [12, 48] and Fuse B has segments [5, 15, 40]:
t = 0: Ignite Fuse A (both ends) -> drains 2 fuel-min/min -> finishes at t = 30.
Ignite Fuse B (one end) -> drains 1 fuel-min/min.
t = 30: Fuse A burns out!
Fuse B's single flame cleared 5 + 15 + 10 = 30 fuel-minutes,
leaving 60 - 30 = 30 fuel-minutes unburned inside its 40-min segment.
Action: Ignite Fuse B's other end!
t = 45: Two flames drain Fuse B's remaining 30 fuel-minutes at 2/min -> 30 / 2 = 15 min.
Fuse B burns out at 30 + 15 = 45 minutes!
Complexity
- Time:
O(1).measure_45_minutes()builds and returns a fixed five-elementList[str]with zero loops or input-dependent scaling. (Executing the physical experiment takes45minutes of wall-clock time.) - Space:
O(1). Memory holds five constant strings.
Edge cases
- Extreme density skew (
1minute in99%of length,59minutes in1%): Two-ended ignition still takes30minutes (1minute until the fast flame reaches the knot, then58 / 2 = 29minutes with both flames sharing the knot). - Simultaneous
t = 0start: Both FuseA(both ends) and FuseB(one end) must ignite att = 0so FuseBhas drained30fuel-minutes when FuseAburns out. - Same-timestamp ordering at
t = 30:"t=30: fuse A burns out."precedes"t=30: ignite other end of fuse B."because the burnout causes the ignition. - Exact template punctuation: Periods, colons, and
"both ends"/"one end"/"other end"must match the specified grammar.
Common mistakes
- Estimating progress by length: Assuming half the length equals
30minutes breaks on variable-density fuses. - Igniting both ends of both fuses at
t = 0: Both fuses burn out simultaneously att = 30, leaving nothing to time the final15minutes. - Leaving Fuse
Bunlit untilt = 30: A full60-minute fuse started att = 30cannot finish earlier thant = 60. - Igniting at an unobservable timestamp: Writing
"t=15: ignite..."when nothing burns out att = 15implicitly assumes a clock.
Follow-up questions
- Can you time a 15-minute interval with just a single 60-minute non-uniform fuse, and what extra lighting rule would make it possible? Under tip-only ignition,
15minutes is impossible from a single60-minute fuse because the lone observable event aftert = 0is final burnout att = 30(both tips) ort = 60(one tip). If the rules permit igniting interior points anywhere along the fuse, ignite both tips plus one interior point att = 0: an interior ignition spawns2outward-moving flames alongside the2tip flames for4simultaneous flames (4fuel-minutes consumed per clock minute). Whenever any sub-piece finishes and two flames annihilate, immediately strike a new interior flame on a surviving segment to keep4active flames running until all60fuel-minutes vanish at60 / 4 = 15minutes. - With three 60-minute non-uniform fuses available instead of two, what is the shortest exact duration you can time? You can measure
7.5minutes (7minutes30seconds): att = 0, ignite both ends of FuseA, one end of FuseB, and one end of FuseC; att = 30(FuseAout), ignite FuseB's other end; att = 45(FuseBout), FuseCholds60 - 45 = 15remaining fuel-minutes, so ignite FuseC's other end and measure the15 / 2 = 7.5-minute interval fromt = 45until FuseCfinishes att = 52.5. Under tip-only rules, each extra fuse halves the finest dyadic step (30,15,7.5, ...,60 / 2^nminutes fornfuses). - Suppose one fuse takes 60 minutes end-to-end while the other takes only 30 minutes—how does assigning which ends to ignite at t=0 change the outcome? Role assignment now alters the total duration! To reach
45minutes fromt = 0, you can either burn them back-to-back (60-minute fuse at both ends for30minutes, followed by the30-minute fuse at both ends for15minutes, or vice versa) or in parallel by igniting one end of each att = 0and lighting the60-minute fuse's other end when the30-minute fuse finishes att = 30(30 + 30 / 2 = 45). Conversely, igniting both ends of the30-minute fuse and one end of the60-minute fuse att = 0triggers burnout att = 15with45fuel-minutes left on the60-minute fuse—lighting its second end then finishes at15 + 45 / 2 = 37.5minutes rather than45(whereas leaving it burning on one end measures45minutes betweent = 15andt = 60).
Follow-up questions
- Can you time a 15-minute interval with just a single 60-minute non-uniform fuse, and what extra lighting rule would make it possible?
- With three 60-minute non-uniform fuses available instead of two, what is the shortest exact duration you can time?
- Suppose one fuse takes 60 minutes end-to-end while the other takes only 30 minutes—how does assigning which ends to ignite at t=0 change the outcome?
Similar questions
- Longest Palindromic Substring
- Descending Key Map Accumulator
- Safely Extract Inner XML Attributes from CDATA Envelopes
- Delivering a Beginner-Friendly Computer Networking Mini-Lesson
- 2D Max Pooling with Source Coordinates
- Time-Limited Credit Ledger