Practice Print FooBar Alternately
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Two threads share a FooBar object initialized with n. One thread calls foo(), which must print "foo" n times, and the other calls bar(), which must print "bar" n times. Synchronize the methods so that their combined output is exactly "foobar" repeated n times, beginning with "foo".
Source: Print FooBar Alternately on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
Input
n = 1
Output
"foobar"
# Example 2
Input
n = 2
Output
"foobarfoobar"
Constraints
1 <= n <= 1000
Related LeetCode Problems
Practice more problems similar to Print FooBar Alternately.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.