2
\$\begingroup\$

I have done some programming tasks as a part of my candidacy for a web development apprenticeship. Unfortunately, my application was rejected as the hiring manager "didn't like my codes." I called up their office but no one was able to provide sufficient feedback for me.

If any of you could tell me what I could have misunderstood or what I could have done correctly, I pretty much appreciate it.

The Tasks I worked with

Write a program that shows the numbers from 1 to a specified limit (e.g. 100).

The numbers should be translated as follows: 1. For multiples of 3 return "Fizz" 2. For multiples of 5 return "Buzz" 3. For multiples of 3 and 5 return "FizzBuzz" 4. If none of the other rules apply, return the number itself

Unit tests are optional, but would be a plus.

My Solution

public class FizzBuzz {
    
    //a class *variable*, not a class
    private static int counter;

    //checking if a multiple of 3
    private static boolean isFizz(int i) {
        return i % 3 == 0; //no need for if
    }

    //checking if a multiple of 5
    private static boolean isBuzz(int i) {
        return i % 5 == 0; //no need for if
    }

    public static void main(String[] args) {
        for (int i = 1; i <= 100; i++) {
            System.out.println(i);
            if (i % 5 == 0 && i % 3 == 0) {
                System.out.println("FizzBuzz");
            } else if (i % 3 == 0){
                System.out.println("Fizz");
            } else if (i % 5 == 0) {
                System.out.println("Buzz");
            } else {
                System.out.println(i);
            }
        }
    }

    @Test public void testFizzBuzz(){
        assertEquals("fizz", fizzBuzz(i == 3));
        assertEquals("buzz", fizzBuzz(i == 5));
        assertEquals("fizzbuzz", fizzBuzz(i == 15));
        assertEquals("2", fizzBuzz(i == 2)); 
    }

}
\$\endgroup\$
3

2 Answers 2

4
\$\begingroup\$

You don't use counter, isFizz() or isBuzz(), testFizzBuzz() calls to a fizzBuzz method which doesn't appear to exist. (Passing a boolean argument which seems illogical).

Your code is formatted well, but the points above mean that a reviewer would doubt your competence.

Have you shown us all your code? If not, why not?

Did you compile, run and test your code? If not, why not?

\$\endgroup\$
4
  • \$\begingroup\$ Have you shown us all your code? If not, why not? Yes I have. Did you compile, run and test your code? If not, why not? Yes I have and I confirmed that it fired correctly on my side. \$\endgroup\$
    – morry239
    Mar 16, 2021 at 13:21
  • 2
    \$\begingroup\$ I don't like to accuse anyone of falsehoods, but that simply can't be the case - that code will not compile, for multiple reasons. Let's start at the first one that my IDE detected, that there is no variable 'i' to test in your test. If I fix that, there's no fizzBuzz(boolean) method. If you don't understand why we're objecting to the code and why the hiring manager rejected it, you need to spend a lot more time learning Java. If the code as presented did indeed compile and run, I think you must have a very strange toolchain! \$\endgroup\$ Mar 16, 2021 at 13:40
  • \$\begingroup\$ Yeah, seems to me like I have a very strange toolchain. \$\endgroup\$
    – morry239
    Mar 16, 2021 at 15:07
  • \$\begingroup\$ See my answer below. \$\endgroup\$
    – morry239
    Mar 19, 2021 at 22:18
1
\$\begingroup\$

Super short review;

  • Your code should be mostly in nicely named methods, not main
  • You call System.out.println(i); twice, which means you do not follow the fizzbuzz rules
  • counter is not used, and I am not sure it should have been declared static
  • Why write a test for isFizz/isBuzz and then never actually use them, this looks bizarre to an interviewer

Also, FizzBuzz is a super common interview question. Do some googling, do some youtubing, you will learn a ton.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for the "well-mannered" response. See my answer below. \$\endgroup\$
    – morry239
    Mar 19, 2021 at 22:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.