© William Gant 2019
William GantSurviving the Whiteboard Interviewhttps://doi.org/10.1007/978-1-4842-5007-5_4

4. Code Katas

William Gant1 
(1)
Nashville, TN, USA
 

In the previous chapter, we discussed using code katas to help prepare yourself for an interview. However, there is a little bit more to it than just simple preparation. In this chapter, we’ll discuss some ideas for fully unlocking the potential of code katas. If you do code katas well, you can not only prepare for an interview more effectively but you can also improve general coding skills. In this chapter, we’ll explain a simple code kata, using the FizzBuzz coding exercise.

I personally use code katas regularly to improve skills in various areas. Because they are usually simple practice problems, you can easily fit them into your schedule without much effort. Because I use C# professionally, I’ve done the following with code katas. You may have to adjust these ideas for your own software stack, but they might give you some ideas of your own. Use code katas to make the following improvements in your software development:
  • Implement test-driven development practices or TDD.

  • Learn to use the Task API and the async primitives.

  • Do the whole thing without using a mouse.

  • Write the code in fewer lines of code, or even with fewer characters.

  • Solve the coding problem in less time.

  • Use a different language to solve the coding problem (I’ve done C++, JavaScript, Ruby, and Python).

  • Learn to use a different IDE for the same language.

  • Learn to use new IDE extensions and plug-ins for part of the process.

  • Practice debugging shortcuts.

  • Use a different Object Relational Mapper (ORM), or database server.

  • Use a different front-end library to accomplish the same task.

You can really squeeze a lot of value out of a code kata besides just learning how to solve a particular problem. In particular, code katas are a nice way to familiarize yourself with your development environment and the various tools that you are using, without the burden of a large and ugly project in the mix. Additionally, they can help you test out a new library quickly, instead of trying it in your main application, where the work is liable to take far longer.

FizzBuzz

In a moment , we’re going to go through the most basic whiteboard problem out there. That problem is called “FizzBuzz” and is the problem I mentioned in Chapter 3. Essentially, solving the problem shows that you have a basic understanding of how to think like a programmer (and not much else). In the next section, I’ll walk you through the process of completing this exercise in a number of different common programming languages. Don’t worry if your language of choice isn’t represented—the point is to understand the process of working through this problem, not to get hung up on the language.

The General Idea

Essentially, the FizzBuzz problem is to write a function that does the following:
  • Loop through the numbers between 1 and 100

  • If the number is divisible by 3, print “Fizz”

  • If the number is divisible by 5, print “Buzz”

  • If the number is divisible by 3 and 5, then print “FizzBuzz”

  • Otherwise just print the number

This process shows that you understand how looping constructs and flow control statements work. It also shows that you understand things like the modulo and equality operators, outputting text to the console, and string manipulation. Pay special attention to how this code was formatted. I tried to make it idiomatic for the language. This serves two purposes. First, it makes it easier on the interviewer to read your code, and second, it doesn’t immediately irritate long-time users of the language in question. You should try to do the same when you are working through whiteboard problems, for the same reasons.

C#

In C#, a developer might be expected to solve this problem as follows:

C# Code
public void FizzBuzz()
{
  for(var i = 1; i <= 100; i++)
  {
      if(i % 15 == 0){
        Console.WriteLine("FizzBuzz");
      }
      else if(i % 5 == 0){
        Console.WriteLine("Buzz");
      }
      else if(i % 3 == 0){
        Console.WriteLine("Fizz");
      }
      else{
        Console.WriteLine(i.ToString());
      }
  }
}

C++

The way a C++ developer might handle this is fairly similar to C#, with the obvious differences in the way console output is done and variables are declared. Note that this assumes that you have imported the standard namespace (std).

C++ Code
#include <iostream>
using namespace std;
void FizzBuzz()
{
  for(int i=1;i<100;i++)
  {
    if(i%15==0)
    {
      cout <<"FizzBuzz" << endl;
    }
    else if(i%5==0) {
      cout <<"Buzz" << endl;
    }
    else if(i%3==0) {
      cout <<"Fizz" << endl;
    }
    else
    {
      cout << i << endl;
    }
  }
}
public int main()
{
   FizzBuzz();
}

PHP

In PHP, you might have the following function body. Note that instead of outputting to a console, PHP folks are more likely to output to a web page. They do this because it’s something PHP is particularly good at. It’s right there in the name (PHP: HyperText Preprocessor—they used a recursive acronym).

PHP Code
for($i = 1; $i <= 100; $i++)
{
  if($i % 15 == 0){
    echo "FizzBuzz<br/>";
  }
  else if($i % 5 == 0){
    echo "Buzz<br/>";
  }
  else if($i % 3 == 0){
    echo "Fizz<br/>";
  }
  else {
    echo $i."<br/>";
  }
}

Ruby

Ruby has a lot of things to help you make your code shorter. If you can fluently whip these out during an interview, it’ll go a long way to showing your skills. I don’t use Ruby in production, but this sort of stuff is why I’ll always have a soft spot in my heart for this language.

Ruby Code
(1..100).each do |n|
  a = String.new
  a << "Fizz" if n%3 == 0
  a << "Buzz" if n%5 == 0
  a << n.to_s if a.empty?
  puts a
end

JavaScript

In JavaScript , one might solve the problem thus. Note the triple equal sign. Also note the use of the “let” keyword. This is for ECMAScript 6. If they are using an older version, this would be “var” instead, so be sure to ask. If you don’t know why the triple equal sign is important or how “let” differs from “var,” be sure and take some time to look that up and get your head around it.

JavaScript Code
function FizzBuzz(){
  for(let i = 1; i <= 100; i++){
    if(i % 15 === 0){
      console.log("FizzBuzz");
    }
    else if(i % 5 === 0){
      console.log("Buzz");
    }
    else if(i % 3 === 0){
      console.log("Fizz");
    }
    else{
      console.log(i.toString());
    }
  }
}

Python

Python is also a beautiful language that I don’t get to use in production. Like Ruby, there is an eloquence to the code that some of these other languages miss. However, in this case, you might not want to get too slick with it.

Python Code
def fizzbuzz():
  for n in range(0,100):
    output = ""
    if(n % 3 == 0):
      output += 'Fizz'
    if(n % 5 == 0):
      output += 'Buzz'
    if(output == ""):
      output += str(n)
    print(output)
fizzbuzz()

Summary

In this chapter, we discussed some practical ways to use code katas to improve your skills. We first discussed some particular areas that can be improved by judicious use of code katas. Next, we worked through a very simple code kata that might also show up when you are given a whiteboard problem during an interview. While this was a very simple problem, it’s exactly the kind of problem that can be a perfect practice problem for improving your development skills.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.216.163