</>

1114. Print in Order

Easy
↗ Open on LeetCode Source: Misc

Quick review

Pattern
Math & Geometry
Time
—
Space
—

Standard Solution (C++)

SummaryGiven three functions that must execute in a fixed order across concurrent threads, ensure `first` always runs before `second`, and `second` always runs before `third`, regardless of thread scheduling.

Approach
  • Use a mutex and condition variable to synchronize the three methods.
  • A shared integer state tracks which step is ready to execute (starts at 1).
  • second and third block on the condition variable until state reaches 2 and 3 respectively, using a predicate lambda to guard against spurious wakeups.
  • After each method prints, it increments state and calls notify_all() to wake any waiting threads.
#include <functional>
#include <mutex>
#include <condition_variable>
using namespace std;

class Foo {
private:
    mutex mtx;
    condition_variable cv;
    int state = 1; // 1 → first ready, 2 → second ready, 3 → third ready

public:
    Foo() {}

    void first(function<void()> printFirst) {
        unique_lock<mutex> lock(mtx);
        // No waiting: first is always allowed to run immediately.
        printFirst();
        state = 2;
        cv.notify_all();
    }

    void second(function<void()> printSecond) {
        unique_lock<mutex> lock(mtx);
        // Block until first() has completed.
        cv.wait(lock, [this] { return state == 2; });
        printSecond();
        state = 3;
        cv.notify_all();
    }

    void third(function<void()> printThird) {
        unique_lock<mutex> lock(mtx);
        // Block until second() has completed.
        cv.wait(lock, [this] { return state == 3; });
        printThird();
    }
};
Time O(1) Space O(1)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

#include <functional>
#include <mutex>
#include <condition_variable>
using namespace std;

class Foo {
private:
    int count=1;
    std::mutex mtx;
    std::condition_variable cv;

public:
    Foo() {
    }

    void first(function<void()> printFirst) {
        std::unique_lock<std::mutex> lock(mtx);
        // printFirst() outputs "first". Do not change or remove this line.
        printFirst();
        count=2;
        cv.notify_all();
    }

    void second(function<void()> printSecond) {
        std::unique_lock<std::mutex> lock(mtx);
        cv.wait(lock, [this]{return count==2;});

        // printSecond() outputs "second". Do not change or remove this line.
        printSecond();

        count=3;
        cv.notify_all();
    }

    void third(function<void()> printThird) {
        std::unique_lock<std::mutex> lock(mtx);
        cv.wait(lock, [this]{return count==3;});
        
        // printThird() outputs "third". Do not change or remove this line.
        printThird();
    }
};

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground C++

Your solution. A driver couldn't be auto-generated for this signature — add a main() that calls the method and prints the result, then Run.

C++ ▶ Runnable Couldn't auto-generate a driver for this signature — edit to add one.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern