A New Programmer’s Guide to Mastering Code Katas

What’s a code kata?  In karate, a kata is a choreographed set of movements to be practiced repetitively.  Unlike karate, code katas don’t need to be repeated over and over again–but they are meant to help programmers reinforce concepts and practice writing code!  Some code katas do focus on repetition to practice test-driven development, IDE shortcuts, or other programming-related skills, but for the purpose of this article, code katas will be defined as “short problems meant to hone your problem-solving ability and improve your coding skills”.  Katas are also a common part of technical interviews, where you’ll be asked to implement a solution using code on a whiteboard or in a text editor. Solving a code kata involves three steps: identifying the parts of a problem, building an algorithm, and implementing the solution algorithm in code. 

Step One: Understanding the Problem

Read the kata carefully, and make a special note of the input and expected output.  If the kata asks you to write a function, make sure that you write a complete function, including the function signature.

Given an array of ints, return true if the array contains two 7’s next to each other, or there are two 7’s separated by one element, such as with {7, 1, 7}.

Step Two: Make A Plan 

Before you start coding, think about how you’ll approach solving the problem.  Which data structures and/or algorithms could you use to solve it? It may also help to divide the problem into smaller problems or steps.  Doing this helps make the problem more manageable and lets you solve only part of the problem at a time. 

It may be helpful to walk yourself through the steps you’ll use to solve the problem, and write out those steps before starting to write actual code.

public boolean has77(int[] nums) {

  //loop through the array 

  //if i is a 7, check i + 1 for 7 and i + 2 for a 7

  //if yes return true

  //else return false 

}

Step Three: Execute Your Plan

It’s time to start coding!  Write the appropriate code for each part of the solution you wrote down in the step above.  When you’re done writing the code, test it with different input values to make sure it works as expected, and fix any problems you encounter.  You may need to identify and solve additional problems or edge cases, such as what happens if the input to your code is empty, null, negative, or very large.

public boolean has77(int[] nums) {

  //loop through the array 
  for(int i = 0; i < nums.length; i++) { 

    //if i is a 7, check i + 1 for 7 and i + 2 for a 7
    if(nums[i] == 7 && i < nums.length - 1 && nums[i + 1] == 7) { 
      return true;
    }
    else if (nums[i] == 7 && i < nums.length -2 && nums[i+2] == 7) { 
      return true;
    }

  }

  //else return false 
  return false;
}

Tips for working on katas:

  • If you can, work in an IDE or other environment where you can run code, like Replit.  This way, you can test your code with different inputs and see the output of your code matches the expected output of the kata.  
  • If you can’t work in an IDE, try working on a whiteboard or other medium where you can easily erase and try something else.  
  • Resist the urge to look up solutions to katas online!  There are plenty of popular katas that have been solved several different ways, and many solutions are available online.  Only look up solutions to a kata after you are satisfied with your own work. 
  • When solving a difficult kata, you may need to look up information on how to use a particular algorithm or how to implement a certain data structure in your language of choice.  It is okay to do research while solving katas!  Research carefully and try to really learn the information you look up, so that you can use it again to solve a future problem. 

In conclusion…

Code katas are meant for learning and practice, so don’t be discouraged if you get stuck!  It’s important to approach solving katas with patience and perseverance.  If a kata is too difficult for now, put it aside and come back to it later.  When you keep practicing, your problem-solving skills will improve, and you’ll find it easier to solve katas.  (Psst–if you need to take a break from programming, there are non-code ways to practice problem-solving, too!)  

Remember, practice makes perfect.  The more you practice using code to solve problems, your programming ability will get stronger, and you’ll discover that solutions will come to you more easily and/or quickly.  Happy coding! 

Add a Comment