AlgoMaster Logo
AlgoMasterPrint FooBar Alternatelymedium

Print FooBar Alternately

medium

Two threads share one FooBar instance:

  • Thread A calls foo(printFoo).
  • Thread B calls bar(printBar).

The printFoo callback outputs "foo", while printBar outputs "bar". Modify the FooBar class so the two threads produce "foobar" exactly n times.

The judge creates the shared object, starts both threads, and calls each method once. Your implementation should coordinate those method calls rather than create threads itself.

The judge also preloads the standard concurrency and callback APIs for each supported language. You do not need to add import, include, or using statements.

Example 1:

Input:

Output:

Example 2:

Input:

Output:

Constraints

  • 1 <= n <= 1000
  • foo and bar are called concurrently on the same FooBar instance.
  • The supplied callbacks are thread-safe and should each be invoked exactly n times.
Loading...

Input

n = 1

Output

foobar

Run is a quick check against the first couple of scenarios, which is roughly what these examples describe. Submit puts your class under the full set, which stays hidden.