Alternate A and B with Two Workers
Updated
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.
More NVIDIA Onsite Interview questions
- Check Endianness Of A Byte ArrayONSITE INTERVIEW · Seen Sep 2026
- Implement MallocONSITE INTERVIEW · Seen Sep 2026
- Implement PrintfONSITE INTERVIEW · Seen Sep 2026
- Merge Two Arrays Into Sorted OrderONSITE INTERVIEW · Seen Sep 2026
- Middle Node Of A Linked ListONSITE INTERVIEW · Seen Sep 2026
- Ping Reply TrackingONSITE INTERVIEW · Seen Sep 2026
- Reverse A StringONSITE INTERVIEW · Seen Sep 2026
- Maximum Fish in a GridONSITE INTERVIEW · Seen Sep 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- First Register Overflow TickONSITE INTERVIEW · Seen May 2024
Similar questions at other companies
- Swap Adjacent in LR StringGoogle · ONSITE INTERVIEWGoogle interview questions
- Travelers per Consecutive City TripletUber · ONSITE INTERVIEWUber interview questions
- Concurrent Buffered File LoggerDatabricks · ONSITE INTERVIEWDatabricks interview questions
- Invoice / Payment ReconciliationStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Classify Password StrengthHackerRank · OA · PHONE SCREEN · ONSITE INTERVIEWHackerRank interview questions
- Highest Bid Campaign MatcherStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Group AnagramsAmazon Web Services · ONSITE INTERVIEWAmazon Web Services interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Response Time Stats by URL PatternGusto · ONSITE INTERVIEWGusto interview questions