{ "data": { "question": { "questionId": "1316", "questionFrontendId": "1195", "boundTopicId": null, "title": "Fizz Buzz Multithreaded", "titleSlug": "fizz-buzz-multithreaded", "content": "
You have the four functions:
\n\nprintFizz
that prints the word "Fizz"
to the console,printBuzz
that prints the word "Buzz"
to the console,printFizzBuzz
that prints the word "FizzBuzz"
to the console, andprintNumber
that prints a given integer to the console.You are given an instance of the class FizzBuzz
that has four functions: fizz
, buzz
, fizzbuzz
and number
. The same instance of FizzBuzz
will be passed to four different threads:
fizz()
that should output the word "Fizz"
.buzz()
that should output the word "Buzz"
.fizzbuzz()
that should output the word "FizzBuzz"
.number()
that should only output the integers.Modify the given class to output the series [1, 2, "Fizz", 4, "Buzz", ...]
where the ith
token (1-indexed) of the series is:
"FizzBuzz"
if i
is divisible by 3
and 5
,"Fizz"
if i
is divisible by 3
and not 5
,"Buzz"
if i
is divisible by 5
and not 3
, ori
if i
is not divisible by 3
or 5
.Implement the FizzBuzz
class:
FizzBuzz(int n)
Initializes the object with the number n
that represents the length of the sequence that should be printed.void fizz(printFizz)
Calls printFizz
to output "Fizz"
.void buzz(printBuzz)
Calls printBuzz
to output "Buzz"
.void fizzbuzz(printFizzBuzz)
Calls printFizzBuzz
to output "FizzBuzz"
.void number(printNumber)
Calls printnumber
to output the numbers.\n
Example 1:
\nInput: n = 15\nOutput: [1,2,\"fizz\",4,\"buzz\",\"fizz\",7,8,\"fizz\",\"buzz\",11,\"fizz\",13,14,\"fizzbuzz\"]\n
Example 2:
\nInput: n = 5\nOutput: [1,2,\"fizz\",4,\"buzz\"]\n\n
\n
Constraints:
\n\n1 <= n <= 50
Compiled with clang 11
using the latest C++ 17 standard.
Your code is compiled with level two optimization (-O2
). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.
Most standard library headers are already included automatically for your convenience.
\"], \"java\": [\"Java\", \" OpenJDK 17
. Java 8 features such as lambda expressions and stream API can be used.
Most standard library headers are already included automatically for your convenience.
\\r\\nIncludes Pair
class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.
Python 2.7.12
.
Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.
\\r\\n\\r\\nFor Map/TreeMap data structure, you may use sortedcontainers library.
\\r\\n\\r\\nNote that Python 2.7 will not be maintained past 2020. For the latest Python, please choose Python3 instead.
\"], \"c\": [\"C\", \"Compiled with gcc 8.2
using the gnu99 standard.
Your code is compiled with level one optimization (-O1
). AddressSanitizer is also enabled to help detect out-of-bounds and use-after-free bugs.
Most standard library headers are already included automatically for your convenience.
\\r\\n\\r\\nFor hash table operations, you may use uthash. \\\"uthash.h\\\" is included by default. Below are some examples:
\\r\\n\\r\\n1. Adding an item to a hash.\\r\\n
\\r\\nstruct hash_entry {\\r\\n int id; /* we'll use this field as the key */\\r\\n char name[10];\\r\\n UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n\\r\\n\\r\\n\\r\\n
2. Looking up an item in a hash:\\r\\n
\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n struct hash_entry *s;\\r\\n HASH_FIND_INT(users, &user_id, s);\\r\\n return s;\\r\\n}\\r\\n\\r\\n\\r\\n\\r\\n
3. Deleting an item in a hash:\\r\\n
\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n HASH_DEL(users, user); \\r\\n}\\r\\n\\r\\n\"], \"csharp\": [\"C#\", \"\\r\\n\\r\\n
Your code is compiled with debug flag enabled (/debug
).
Python 3.10
.
Most libraries are already imported automatically for your convenience, such as array, bisect, collections. If you need more libraries, you can import it yourself.
\\r\\n\\r\\nFor Map/TreeMap data structure, you may use sortedcontainers library.
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }