FastPrepAlternate A and B with Two Workers
Problems/Alternate A and B with Two Workers

Alternate A and B with Two Workers

NVIDIA logoNVIDIA● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an NVIDIA onsite interview for a full-time role. The report asks two threads to take turns printing a and b, and mentions an approach with two locks. We chose a finite length, a returned buffer, and Go goroutines.

Our version is about 80% the same as the reported question.

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) → String

Examples

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 2n characters 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

NVIDIA interview experiences

  • NVIDIA Full-Time SWE Onsite Scheduling: 1 Experience
  • NVIDIA OA: What It Tests (2026)
  • NVIDIA DevOps, QA and Infra Intern Interviews: 12 Experiences
  • NVIDIA Ignite Internship OA Experience
  • NVIDIA Software Intern Interview Questions: 9 Experiences
  • NVIDIA Ignite SWE Intern OA Questions: 6 Experiences
All NVIDIA interview questionsSee NVIDIA hiring insights
Saved
class Solution {
    public String alternateLetters(int n) {
        // Start two concurrent workers and return their shared output.
    }
}
Checking account…