You're checking for a null local variable value in the third conditional, but appending to that same variable in the first two, which would either cause a runtime error in most languages or never pass the third conditional.
In Ruby, for example, you could append a string to nil by interpolating the nil into a string literal, or various other ways. Seeing as he didn't specify the language, what the variable was initialized to, or what exactly "append" means, we can only assume that these imaginary things behave as intended.
This is my conclusion as well. To wit, an approximate translation of his method into Ruby:
fizzbuzz = (1..100).to_a.map do |i|
result = "" # 1:23 initialize a variable
result += "Fizz" if i % 3 == 0
result += "Buzz" if i % 5 == 0
result = i.to_s if result.nil?
result
end
The corrected solution would be to change `result.nil?` to `result.empty?`, i.e. checking for "empty string" instead of "null".
Well, that would depend on the print statement. Generally, all of them have new lines. Even if they don't you would have to generate new lines appropriately otherwise the next number would print right next to Fizz.
PS : My assumption is that you need to print each number or Fizz or Buzz or FizzBuzz in a separate line.
what? for this task, programmers should be able to come up with code optimized (if not optimal) right away.
For example, this line "I would append a string [...] to the result" certainly involves at least one memory copy operation, especially since he suggested doing this for mod 3 and mod 5.
Everyone should know that on most CPUs, arithmetic operations are at least an order of magnitude faster than memory operations.
So the alternative should be obvious: don't copy the strings into a string buffer. If you just print the 2 strings ('Fizz' and 'Buzz') as necessary (inside the conditional test blocks) the compiler/interpreter will allocate space for these 2 strings ONLY and will pass pointers to them.
Even though the last operation he mentioned "I would check if the string is null" would indeed be fast, the program won't be able to take advantage of it, because by that time, the program will be performing very costly memory operations.
Instead, this involves no extra memory copy:
for i in 1 to 100:
if (i mod 3): print 'Fizz';
if (i mod 5): print 'Buzz';
if (not (i mod 3) and not (i mod 5)): print i;
print "\n";
Even though the 3rd condition involves a few extra arithmetic/logical operations, these are way faster than memory operations. The strings 'Fizz', 'Buzz' and "\n" are allocated at the beginning of the program. There is no "result" variable that needs to be allocated/deallocated on every iteration and there is also no need to store the result in an array, because the task is to write a program that "prints" the numbers from 1 to 100. Plus, wouldn't you want to store it in a string, instead of allocating memory for arrays?
I would expect good candidates to be able to at least arrive at this solution after their first attempt.
16 comments
[ 3.2 ms ] story [ 87.1 ms ] threadHe goes on to append strings to this variable which would lead me to believe it has some value that you can append to.
This would make the third conditional where he checks for null to fail, he should check for an empty string.
Forcing anyone to do a coding exercise in their head is ridiculous.
PS : My assumption is that you need to print each number or Fizz or Buzz or FizzBuzz in a separate line.
He doesn't ever print out result either
Even though the last operation he mentioned "I would check if the string is null" would indeed be fast, the program won't be able to take advantage of it, because by that time, the program will be performing very costly memory operations.
Instead, this involves no extra memory copy:
for i in 1 to 100: if (i mod 3): print 'Fizz'; if (i mod 5): print 'Buzz';
Even though the 3rd condition involves a few extra arithmetic/logical operations, these are way faster than memory operations. The strings 'Fizz', 'Buzz' and "\n" are allocated at the beginning of the program. There is no "result" variable that needs to be allocated/deallocated on every iteration and there is also no need to store the result in an array, because the task is to write a program that "prints" the numbers from 1 to 100. Plus, wouldn't you want to store it in a string, instead of allocating memory for arrays?I would expect good candidates to be able to at least arrive at this solution after their first attempt.