Alternate A and B with Two Workers
Problem statement
Coordinate two concurrent workers that produce alternating letters. One worker writes only a; the other writes only b. The combined sequence starts with a and alternates strictly between the workers.
For this exercise, given n, each worker must append its letter exactly n times to one shared buffer. Start both workers, wait for both to finish, and return the buffer as a string. Use threads in Python, Java, and C++, or goroutines in Go. Synchronize the workers regardless of which is scheduled first; do not build the alternating string in one sequential worker.
Function
alternateLetters(n: int) → StringExamples
Example 1
n = 3return = "ababab"Each worker contributes three letters, with the a worker going first.
Example 2
n = 0return = ""Neither worker appends a character, so the shared buffer stays empty.
Example 3
n = 1return = "ab"One handoff produces a followed by b.
Constraints
0 <= n <= 1000.- For
n = 0, return the empty string. - The returned string has exactly
2ncharacters and contains no spaces or newline.