Print FooBar Alternately
Updated
Problem statement
Given an integer n, run two workers at the same time and return the text they write together. One worker writes "foo" exactly n times and the other writes "bar" exactly n times, both into one shared output. They must take turns so the output is "foobar" repeated n times: a "foo" comes first, and every "bar" comes right after a "foo".
In Java, Python and C++, run each worker in its own thread. In Go, run each worker in its own goroutine. In JavaScript, run each worker in its own worker thread. Use synchronization, such as a lock with a condition variable, semaphores or channels, so the order is right no matter which worker starts running first. Wait for both workers to finish before you return the output.
Note: only the returned string is checked. Building it in one worker, or with no workers at all, gives the same string but skips the turn-taking this question is about.
Function
fooBar(n: int) → StringExamples
Example 1
n = 3return = "foobarfoobarfoobar"The "foo" worker writes first, then the "bar" worker, and they keep taking turns. Each writes its word 3 times, so the output is "foobar" three times.
Example 2
n = 5return = "foobarfoobarfoobarfoobarfoobar"Each worker writes its word 5 times, so the output has 10 words and 30 letters: "foobar" five times.
Constraints
1 <= n <= 1000
More Nutanix Phone Screen questions
- Swap Adjacent Linked List NodesPHONE SCREEN · Seen Mar 2025
- Top K Most Frequent ValuesPHONE SCREEN · Seen Mar 2025
- Unique Triplets Summing to ZeroPHONE SCREEN · Seen Sep 2024
- Find the Duplicate in a Read-Only ArrayPHONE SCREEN · Seen Apr 2021
- Kth Largest Element in an ArrayPHONE SCREEN · Seen Feb 2020
- Kth Smallest Element in a BSTPHONE SCREEN · Seen Feb 2020
- Outline of a Binary TreePHONE SCREEN · Seen Jan 2020
- Print All File and Folder PathsPHONE SCREEN · Seen Dec 2019
- Inorder Predecessor in a BSTPHONE SCREEN · Seen Jan 2019
- K Largest and K Smallest ValuesPHONE SCREEN · Seen Jan 2019
Similar questions at other companies
- Print Numbers in Turn with Three ThreadsThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Timer Job Scheduler Wake-upsRedNote · PHONE SCREEN
- Detect a Deadlock from Lock LogsHelm.ai · PHONE SCREEN
- Crawl Same-Hostname URLs ConcurrentlyAnthropic · PHONE SCREENAnthropic interview questions
- Rank-Based Cache with a Data SourceLinkedIn · PHONE SCREENLinkedIn interview questions
- Parallel String Counts with a Custom Hash TableNVIDIA · PHONE SCREENNVIDIA interview questions
- Concurrent Discounted Card PurchasesBrex · PHONE SCREEN
- Concurrent Token-Bucket Rate LimiterCharta Health · PHONE SCREENCharta Health interview questions
- Ordered Number Printing with Three ThreadsArize AI · PHONE SCREEN
- Schedule Buffered Video PlaybackNuro · PHONE SCREENNuro interview questions
Interview reports that mention this question
Nutanix interview experiences
- Nutanix SWE Intern Phone Screens: 20 Experiences (Part 3 of 3)
- Nutanix SWE Intern OA Questions: 16 Experiences (Part 2 of 3)
- Nutanix U.S. SWE Intern Interviews: 54 Experiences (Part 1 of 3)
- Nutanix SWE OA and Interviews, Level Not Reported: 9 Experiences
- Nutanix Intern Debugging Round and Interviews: 25 Experiences