Leetcode Problem 1115. Print FooBar Alternately
1115. Print FooBar Alternately
AI Mock Interview
Leetcode Solutions
Using Condition Variables and Mutex
Solution Idea
Algorithm Steps
Code Implementation
Initialize a mutex and two condition variables, one for each function (
foo
and
bar
).
In the
foo
function, acquire the mutex lock.
Print 'foo'.
Signal the condition variable associated with
bar
and release the mutex lock.
In the
bar
function, acquire the mutex lock.
Wait on the condition variable until signaled by
foo
.
Print 'bar'.
Signal the condition variable associated with
foo
and release the mutex lock.
Repeat steps 2-8 for
n
iterations.
Using Semaphores
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...