1115. Print FooBar Alternately LeetCode Solution
In this guide, you will get 1115. Print FooBar Alternately LeetCode Solution with the best time and space complexity. The solution to Print FooBar Alternately problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Print FooBar Alternately solution in C++
- Print FooBar Alternately solution in Java
- Print FooBar Alternately solution in Python
- Additional Resources

Problem Statement of Print FooBar Alternately
Suppose you are given the following code:
class FooBar {
public void foo() {
for (int i = 0; i < n; i++) {
print("foo");
}
}
public void bar() {
for (int i = 0; i < n; i++) {
print("bar");
}
}
}
The same instance of FooBar will be passed to two different threads:
thread A will call foo(), while
thread B will call bar().
Modify the given program to output "foobar" n times.
Example 1:
Input: n = 1
Output: “foobar”
Explanation: There are two threads being fired asynchronously. One of them calls foo(), while the other calls bar().
“foobar” is being output 1 time.
Example 2:
Input: n = 2
Output: “foobarfoobar”
Explanation: “foobar” is being output 2 times.
Constraints:
1 <= n <= 1000
Complexity Analysis
- Time Complexity: Google AdSense
- Space Complexity: Google Analytics
1115. Print FooBar Alternately LeetCode Solution in C++
// LeetCode doesn't support C++20 yet, so we don't have std::counting_semaphore
// or binary_semaphore.
#include <semaphore.h>
class FooBar {
public:
FooBar(int n) : n(n) {
sem_init(&fooSemaphore, /*pshared=*/0, /*value=*/1);
sem_init(&barSemaphore, /*pshared=*/0, /*value=*/0);
}
~FooBar() {
sem_destroy(&fooSemaphore);
sem_destroy(&barSemaphore);
}
void foo(function<void()> printFoo) {
for (int i = 0; i < n; ++i) {
sem_wait(&fooSemaphore);
printFoo();
sem_post(&barSemaphore);
}
}
void bar(function<void()> printBar) {
for (int i = 0; i < n; ++i) {
sem_wait(&barSemaphore);
printBar();
sem_post(&fooSemaphore);
}
}
private:
const int n;
sem_t fooSemaphore;
sem_t barSemaphore;
};
/* code provided by PROGIEZ */
1115. Print FooBar Alternately LeetCode Solution in Java
class FooBar {
public FooBar(int n) {
this.n = n;
}
public void foo(Runnable printFoo) throws InterruptedException {
for (int i = 0; i < n; ++i) {
fooSemaphore.acquire();
printFoo.run();
barSemaphore.release();
}
}
public void bar(Runnable printBar) throws InterruptedException {
for (int i = 0; i < n; ++i) {
barSemaphore.acquire();
printBar.run();
fooSemaphore.release();
}
}
private int n;
private Semaphore fooSemaphore = new Semaphore(1);
private Semaphore barSemaphore = new Semaphore(0);
}
// code provided by PROGIEZ
1115. Print FooBar Alternately LeetCode Solution in Python
from threading import Semaphore
class FooBar:
def __init__(self, n):
self.n = n
self.fooSemaphore = Semaphore(1)
self.barSemaphore = Semaphore(0)
def foo(self, printFoo: 'Callable[[], None]') -> None:
for _ in range(self.n):
self.fooSemaphore.acquire()
printFoo()
self.barSemaphore.release()
def bar(self, printBar: 'Callable[[], None]') -> None:
for _ in range(self.n):
self.barSemaphore.acquire()
printBar()
self.fooSemaphore.release()
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.