Run three methods in a fixed order when three threads call them in any order
Company: Tesla
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
A class `Foo` has three methods: `first`, `second` and `third`. The same instance of `Foo` is passed to three different threads. Thread A calls `first`, thread B calls `second` and thread C calls `third`.
Design a mechanism that guarantees `second` always executes after `first`, and `third` always executes after `second`, regardless of the order in which the operating system schedules the threads.
Each method receives the string that its step records as output (for example `"first"`, `"second"` or `"third"`). A method whose predecessor has not run yet is queued, and it executes as soon as that predecessor completes. Every call returns the list of outputs that have executed so far, in execution order.
Implement these methods:
```text
first(printFirst: str) -> List[str]
second(printSecond: str) -> List[str]
third(printThird: str) -> List[str]
```
- `first` executes the first task and returns the outputs executed so far.
- `second` executes the second task, but only after `first` has completed. If `first` has not run yet, the task is queued. It returns the outputs executed so far.
- `third` executes the third task, but only after `second` has completed. If `second` has not run yet, the task is queued. It returns the outputs executed so far.
Example 1:
```text
foo = Foo()
foo.first("first") -> ["first"]
foo.second("second") -> ["first", "second"]
foo.third("third") -> ["first", "second", "third"]
```
Each call arrives after its predecessor has run, so every step executes immediately.
Example 2:
```text
foo = Foo()
foo.third("third") -> []
foo.second("second") -> []
foo.first("first") -> ["first", "second", "third"]
```
`third` is queued because `second` has not run, and `second` is queued because `first` has not run. When `first` runs it releases `second`, which in turn releases `third`, so the last call returns all three outputs.
```hint Where does a late step go?
When `second` arrives before `first`, it cannot run yet. Decide where to keep it, and which later call is responsible for running it.
```
### Constraints & Assumptions
- Each of `first`, `second` and `third` is called exactly once.
- The methods may be called in any order.
- The returned list always reflects execution order: `first`, then `second`, then `third`.
### Clarifying Questions to Ask
- Are the three calls made one after another by a single test harness, or truly at the same time from three threads?
- May a method block until its predecessor finishes, or must it return right away?
- When a step is queued, may another thread execute it, or must each action run on the thread that called it?
- Is each method called exactly once, or can the first, second, third sequence repeat?
### What a Strong Answer Covers
- State that records which steps have run and which calls are waiting, with the rule that a step runs only after its predecessor.
- Releasing waiting steps as a chain when a predecessor completes, so `first` can release both `second` and `third`.
- Returning a copy of the output list, so values returned earlier do not change later.
- The check-then-act race when the calls really are concurrent, and the lock, event or semaphore that closes it.
- The difference between queuing a step for later and blocking the calling thread until its turn.
### Follow-up Questions
- How would you generalize the class to n steps that must run in order?
- If each thread must block until its turn and then run its own action, which synchronization primitive would you use, and why?
- How would you make the object reusable for many rounds of `first`, `second`, `third`?
- What goes wrong if a thread spins in a loop checking a boolean flag until its predecessor is done?
Overview: Implement a class whose three methods must execute in a fixed order even when three threads call them in any order, queuing early calls and returning the outputs executed so far. It tests ordering state, releasing waiting work in a chain, and thread safety with locks or events.