r/programminghorror • • Feb 28 '26

Do you like my FizzBuzz implementation

Post image
1.3k Upvotes

218 comments sorted by

View all comments

Show parent comment

21

u/[deleted] Feb 28 '26

Yeah, like it’s horror in where it’s going. Another case and we need 8 switches, another and we need 16, etc., but wouldn’t technically call it horror yet until it had more cases. In its current state, it makes me wince but not cry.

I’d leave a review comment saying this makes my eyes hurt and rewrite x way if it was in a PR, but wouldn’t consider it horror.

If it was an interview I’d just ask them how they’d approach adding another case, and make sure that they recognize their current pattern isn’t scalable

27

u/JollyJuniper1993 Feb 28 '26

I don’t think FizzBuzz requires scalability

9

u/ACoderGirl Mar 01 '26

Maybe your FizzBuzz doesn't, but mine needs to be able to handle 20M QPS.

1

u/california_snowhare Mar 03 '26 edited Mar 03 '26

Here you go. Tested to run a bit more than 140MQPS on an Apple M2 Ultra and more than 50MQPS on a Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz.

#include <string>
#include <vector>
using namespace std;

class Solution {
private:
    vector<string> lookup = {
        "FizzBuzz",
        "", "", "Fizz", "", "Buzz", "Fizz", "",
        "", "Fizz", "Buzz", "", "Fizz", "", ""};
public:
    vector<string> fizzBuzz(int n) {
        vector<string> ans;
        ans.reserve(n);
        int mod15_counter = 1;
        for(int i=1; i<=n; i++) {
            ans.push_back(lookup[mod15_counter].length() == 0 ? to_string(i) : lookup[mod15_counter]);
            mod15_counter += mod15_counter == 14 ? -14 : 1;
        }

        return ans;
    }
};

-4

u/[deleted] Feb 28 '26

A problem of this type may, hence why I would ensure they understand the limitation in an interview, and accept their answer if they show that they do.

If this was a code base, it can simply be written cleaner. But, you wouldn’t be PRing FizzBuzz. You’d be PR’ing a problem of this type

8

u/JollyJuniper1993 Mar 01 '26

A decent developer will know when something is a waste of time

1

u/[deleted] Mar 01 '26

So which one do you disagree with, that this is not horror, but could be cleaner if in PR or that this would be acceptable in an interview if candidate was able to identify that this pattern doesn’t scale when I ask?

1

u/Jamie_1318 Mar 01 '26

You say it wouldn't scale well, but it isn't really any worse when adding additional constraints than the traditional if elseif elseif else implementation.

If I were reviewing this I probably wouldn't complain, at worst it would be a nit to name the conditionals or something. With a dict to name the conditions it can scale as far as code realistically should before talking about data-driven implementations or a dedicated class.

Realistically it's a short enough piece of code that it can easily be rewritten later if necessary, and it isn't hairy enough that it would cause a problem to do so.

26

u/csabinho Feb 28 '26

But FizzBuzz is defined as such. And this is a concise version of FizzBuzz.

-11

u/[deleted] Feb 28 '26

Concise FizzBuzz:

for i in range(100):

cases = [(3, "Fizz"), (5, "Buzz")]

result = "".join(substring for mod, substring in cases if i % mod == 0)

print(result or i)

Notice this solution is tighter and more scalable. Coded it on Reddit, so apologies if I have a typo in there.

That aside, as I said, if this was an interview, I would accept this answer, then follow up to make sure they recognize they need to make changes should more cases be added. If I asked them how they would add 2 more cases, and their responses was 16 switches, it’s a problem. If their response is refactor, then it’s completely fine.

If this was a PR, then I we have the time and option to take okayish code and just make it cleaner. So I would just comment solution above.

You won’t actually be putting FizzBuzz in a code base aside from like a test case if you’re writing a compiler or something, so I’m more speaking of if I saw a general problem of this type.

10

u/UltraPoci Feb 28 '26

This is scalable, but what if the spec changes and I want the 15 case to print Foo? Or what if I want to add a 9 case for which it prints Bar, not FizzBar?

8

u/PlebbitDumDum Feb 28 '26

I'd hire OP over you any day.

-1

u/[deleted] Feb 28 '26

Well OP posted this as an example of programming horror, so seems like they like their own solution less than I do lmao

3

u/csabinho Feb 28 '26

If I asked them how they would add 2 more cases, and their responses was 16 switches, it’s a problem. If their response is refactor, then it’s completely fine. 

That's the point.

-1

u/[deleted] Feb 28 '26

Then read last two sentences of my original comment before commenting yourself

4

u/csabinho Feb 28 '26

Those lines sound completely different than the ones I quoted.

-1

u/[deleted] Feb 28 '26

“If it was an interview I’d just ask them how they’d approach adding another case, and make sure that they recognize their current pattern isn’t scalable”

-1

u/[deleted] Mar 01 '26 edited Mar 01 '26

I’d love to hear how that’s completely different😂 But you just disappeared with a down vote🥲. Down voting because you disagree that this can be cleaner in a PR, but in an interview acceptable if candidate was able to correctly identify how this pattern doesn’t scale? Or because you read the wrong comment the first time? Just wanted to verify that this is what you’re arguing about

2

u/le_ramequin Mar 01 '26

stuff can always be refined later when needing to scale

1

u/-TRlNlTY- Mar 02 '26

There is nothing to scale here