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 = 0 or 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 A consume 2 minutes of fuel per clock minute, meeting at t = 30. Meanwhile, one flame on Fuse B consumes 30 minutes of fuel, leaving 30 minutes of fuel unburned; igniting Fuse B's opposite tip at t = 30 halves that remainder to 15 minutes, ending at t = 45.

Example 2

  • Input: Simulate measure_45_minutes() when Fuse A has two equal-length halves taking [12, 48] single-flame minutes and Fuse B has three segments taking [5, 15, 40] single-flame minutes.
  • Output: Fuse A burns out at 30.0 minutes; Fuse B burns out at 45.0 minutes (steps[-1] == "t=45: fuse B burns out.").
  • Explanation: On Fuse A, the left flame clears the 12-minute half at t = 12 while the right flame eats 12 minutes of the 48-minute half; both flames then share the remaining 36 fuel-minutes for 36 / 2 = 18 minutes (12 + 18 = 30). On Fuse B, one flame clears 5 + 15 + 10 = 30 fuel-minutes by t = 30, and two flames finish the remaining 30 fuel-minutes in 30 / 2 = 15 minutes (30 + 15 = 45).

Constraints

  • No external timing or length-measuring instruments exist; ignitions may only occur at t = 0 or 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

  1. Understand the puzzle and event-log contract

    We hold two chemical fuses (A and B) and unlimited matches, without a clock, ruler, or blade. Each fuse burns end-to-end in 60 minutes under a single flame, yet local burn speed varies wildly—for instance, in Example 2 Fuse A's left half takes 12 minutes while its dense right half takes 48 minutes.

    measure_45_minutes() must return the chronological List[str] of "t=<minute>: ..." entries describing how we measure 45 exact minutes from t = 0 to t = 45.

  2. 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 (60 fuel-minutes total).

    • One active flame always consumes 1 fuel-minute per clock minute, whether moving fast across thin material or creeping across a dense knot.
    • Two opposite flames burning simultaneously each consume 1 fuel-minute per clock minute, draining 1 + 1 = 2 fuel-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 T fuel-minutes always finishes in T / 2 clock minutes.

  3. Trigger actions only on observable events

    With no watch on our wrist, we cannot say "wait 15 minutes and strike a match." Only two kinds of moments are directly observable:

    1. The initial start (t = 0), and
    2. The instant an active fuse completely burns out.

    Igniting both ends of Fuse A at t = 0 creates a guaranteed observable signal at t = 30.

  4. Drain Fuse B in parallel to leave 30 fuel-minutes

    Because 45 = 30 + 15 and 15 = 30 / 2, we want Fuse B to arrive at t = 30 with exactly 30 fuel-minutes left so that igniting its opposite end halves that remainder into 15 clock minutes.

    How do we remove 30 fuel-minutes from Fuse B during the first 30 minutes? Ignite one end of Fuse B right at t = 0 alongside Fuse A!

    • Leaving Fuse B unlit until t = 30 leaves all 60 fuel-minutes intact, so Fuse B could only finish at 30 + 30 = 60 (both ends) or 30 + 60 = 90 (one end).
    • Burning one end of Fuse B from t = 0 to t = 30 consumes 30 fuel-minutes, leaving 60 - 30 = 30 fuel-minutes unburned regardless of how uneven Fuse B is.
  5. 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=30 burnout before t=30 ignition, Fuse A before Fuse B at t=0) yields five crisp entries.

  6. 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-element List[str] with zero loops or input-dependent scaling. (Executing the physical experiment takes 45 minutes of wall-clock time.)
  • Space: O(1). Memory holds five constant strings.

Edge cases

  • Extreme density skew (1 minute in 99% of length, 59 minutes in 1%): Two-ended ignition still takes 30 minutes (1 minute until the fast flame reaches the knot, then 58 / 2 = 29 minutes with both flames sharing the knot).
  • Simultaneous t = 0 start: Both Fuse A (both ends) and Fuse B (one end) must ignite at t = 0 so Fuse B has drained 30 fuel-minutes when Fuse A burns 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 30 minutes breaks on variable-density fuses.
  • Igniting both ends of both fuses at t = 0: Both fuses burn out simultaneously at t = 30, leaving nothing to time the final 15 minutes.
  • Leaving Fuse B unlit until t = 30: A full 60-minute fuse started at t = 30 cannot finish earlier than t = 60.
  • Igniting at an unobservable timestamp: Writing "t=15: ignite..." when nothing burns out at t = 15 implicitly 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, 15 minutes is impossible from a single 60-minute fuse because the lone observable event after t = 0 is final burnout at t = 30 (both tips) or t = 60 (one tip). If the rules permit igniting interior points anywhere along the fuse, ignite both tips plus one interior point at t = 0: an interior ignition spawns 2 outward-moving flames alongside the 2 tip flames for 4 simultaneous flames (4 fuel-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 keep 4 active flames running until all 60 fuel-minutes vanish at 60 / 4 = 15 minutes.
  • With three 60-minute non-uniform fuses available instead of two, what is the shortest exact duration you can time? You can measure 7.5 minutes (7 minutes 30 seconds): at t = 0, ignite both ends of Fuse A, one end of Fuse B, and one end of Fuse C; at t = 30 (Fuse A out), ignite Fuse B's other end; at t = 45 (Fuse B out), Fuse C holds 60 - 45 = 15 remaining fuel-minutes, so ignite Fuse C's other end and measure the 15 / 2 = 7.5-minute interval from t = 45 until Fuse C finishes at t = 52.5. Under tip-only rules, each extra fuse halves the finest dyadic step (30, 15, 7.5, ..., 60 / 2^n minutes for n fuses).
  • 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 45 minutes from t = 0, you can either burn them back-to-back (60-minute fuse at both ends for 30 minutes, followed by the 30-minute fuse at both ends for 15 minutes, or vice versa) or in parallel by igniting one end of each at t = 0 and lighting the 60-minute fuse's other end when the 30-minute fuse finishes at t = 30 (30 + 30 / 2 = 45). Conversely, igniting both ends of the 30-minute fuse and one end of the 60-minute fuse at t = 0 triggers burnout at t = 15 with 45 fuel-minutes left on the 60-minute fuse—lighting its second end then finishes at 15 + 45 / 2 = 37.5 minutes rather than 45 (whereas leaving it burning on one end measures 45 minutes between t = 15 and t = 60).

Follow-up questions

Similar questions

More Intersystems interview questions

All 5 Intersystems questions →