DEV Community

Cover image for Interview problem - FizzBuzz
Chris Noring for Microsoft Azure

Posted on

Interview problem - FizzBuzz

So, you try to pass an interview as a programmer. There are a few ways you're tested, it's not just coding skills but your ability to communicate.

Communicate, what do you mean, code either compiles and works or it don't?

Well, no, so you live in a world with stakeholder and team members. In that world, you need to navigate and that means understanding a problem, asking clarifying questions and be prepared for changes and yes sometimes short deadlines.

So what can I expect on the interview then?

Well, for one, a problem, and not just a problem, you're expected to communicate from start to finish, through all the phases.

What phases?

Understand the problem

In this phase, you need to listen, repeat back what you heard and from there communicate assumptions. If those assumptions are incorrect, you may get clarifications. What you don't want to do is to just code away thinking you understood everything.

Problem breakdown

In this phase, you should start to think about how to break down a problem in easier parts, sub problems if you will. At the job, you will be given big and complicated tasks and you will be asked to break it down into smaller parts that you can solve. - That's the job, oh and coding :).

Start coding

At this point, start implementing the easier parts of your problem, a naive solution if you will. See if your interviewer has any comments. Then proceed until you have a working solution.

Cover all your bases

At this point, ensure you communicate any edge cases that you might need to handle and possible improvements to your code.

Here it's likely your interviewer may spot a bug or ask you to implement a new requirement. Don't panic, take your time and figure out how to solve.

Summary

In the last part, the interviewer may sum up the interview and ask general questions, but if you have communicated thus far, you've written reasonable code that mostly works, chances are you pass this interview.

So what problem am I likely to get, can you give m an example?

The problem : Fizz Buzz

Lets take a problem called Fizz Buzz. Here's the description:

Your code is divisible by 5 and 3. If both then print "FizzBuzz", if only by 3, then "Fizz" if only by 5, then "Buzz".

To approach this problem, ensure you understand its sub problems:

1) Divisible, that a number is divided and leaves no remainder and you at least in Python have a module operator to help you.
2) Understand what condition to implement first, i.e. you should start with divisible by 5 and 3 and then add the other conditions.

Here's a solution:

arr = range(30)
for item in arr:
  no = item + 1
  if no % 5 == 0 and no % 3 == 0:
    print(f"{no} FizzBuzz")
  elif no % 5 == 0:
    print(f"{no}: Buzz")
  elif no %3 == 0:
    print(f"{no} Fizz")
  else:
    print(f"{no}")
Enter fullscreen mode Exit fullscreen mode

and yea, take your time implementing and COMMUNICATE cause you're tested on both skills.

Discussion (0)