DEV Community

Cover image for My Top 5 Favorite Algorithm Problems at Codewars
Eduardo Zepeda
Eduardo Zepeda

Posted on • Originally published at coffeebytes.dev on

My Top 5 Favorite Algorithm Problems at Codewars

What is codewars?

Codewars is a social network of programmers who get together to challenge each other to solve code challenges. Codewars is one of the best websites for practicing algorithms and solving Katas. Katas? Yes, as in Karate.

What are katas in codewars?

In the spirit of martial arts, more specifically Karate, these code problems are called katas. The katas are divided, ascendingly, according to their difficulty. There are katas from 8th kyu to 1st kyu, with 1st kyu being the most difficult type of kata of all.

There are katas on many, many topics: algorithm development, efficiency, regex, mathematics, cryptography, etc.

Collectively, the katas encompass a variety of languages: C, C++, Go, Python, Javascript, Elixir, Haskell, Rust, even languages as esoteric as Brainfuck. While, individually, each Kata has one or more languages.

Without further ado, here are my top 5 katas. These katas are not necessarily the most difficult ones , but the ones that I consider to have the ideal balance between creativity and difficulty. I choose those that give that feeling of a good puzzle, one of those that you can’t stop until you solve it.

By the way, no, I’m not going to post the answers , those are up to you.

Multi Line Task++: Hello World

You need to write a function that returns the string “Hello, world!” in Javascript.

Requirement: Each line must have at most 2 characters, and the total number of lines must be less than 40.

Hint: It is possible to complete it in only 28 lines of code.

Original Kata: Multi Line Task++: Hello World

Note on Kata

The difficult part is the two characters per line maximum. Give it a try.

12
34
56
78
//

Enter fullscreen mode Exit fullscreen mode

There is a more complicated version where the limit is one character per line, in case you find this one too easy.

Make a spiral

Your task is to create a spiral of NxN with the given size.

For example, a spiral with 5 sides should look like this:

00000
....0
000.0
0...0
00000

Enter fullscreen mode Exit fullscreen mode

And size 10

0000000000
.........0
00000000.0
0......0.0
0.0000.0.0
0.0..0.0.0
0.0....0.0
0.000000.0
0........0
0000000000

Enter fullscreen mode Exit fullscreen mode

The return value should contain an array of arrays, 0’s and 1’s, with the first row consisting of 1’s. For example, for the given size of 5, it should be:

[[1,1,1,1,1],[0,0,0,0,1],[1,1,1,0,1],[1,0,0,0,1],[1,1,1,1,1]]

Enter fullscreen mode Exit fullscreen mode

For extreme cases of small spirals, the size will be at least 5.

As a general rule, the snake made of 1s cannot touch itself.

Original Kata: Make a spiral

Note on Kata

It looks easy, but I assure you it won’t be so easy on your first try.

The soul of wit: reverse an array

No time for stories, invert an array (in Javascript), return the result. Do whatever you want with the original array. Don’t use Array.prototype.reverse.

You have 30 bytes to spend.

Example: [1, 2, 3] → [3, 2, 1].

This time you won’t be able to do the other Kata thing.

Nor can you use require.

Kata original: The soul of wit: reverse an array

Note on Kata

By 30 bytes it means that you have the equivalent in characters to use in your code. For example: the solution below has 33 characters, it exceeds the limit and also cannot be used reverse.

const reverse = a => a.reverse();

Enter fullscreen mode Exit fullscreen mode

Last digit of a huge number

Given a list [x1, x2, x3, …, xn] compute the last digit (decimal) of x1 ^ (x2 ^ (x3 ^ (… ^ xn))).

Example:

lastDigit([3, 4, 2]) === 1

because 3 ^ (4 ^ 2) = 3 ^ 16 = 43046721.

Beware: powers grow incredibly fast. For example, 9 ^ (9 ^ 9) has more than 369 million digits. Your lastDigit function has to deal with those numbers efficiently.

Unusual cases: we assume that 0 ^ 0 = 1 and that the last digit of an empty list is equal to 1.

Kata original: Last digit of a huge number

Note on Kata

If you are thinking of writing something like:

def lastDigit(arr):
    # Esta función NO es la correcta
    total = 1
    for element in arr[::-1]:
        total = element ** total

    return str(total)[-1]

last_digit([528374,27415,789392,462589,166837,699678,866982])

Enter fullscreen mode Exit fullscreen mode

Solutions like this will get you nowhere, the Kata has to run incredibly fast.

See how long it takes to run in Python with the correct lastDigit function.

time python script.py 

real 0m0.122s
user 0m0.073s
sys 0m0.044s

Enter fullscreen mode Exit fullscreen mode

If you try to run the above code you can probably go make a cup of coffee before it finishes executing.

Escape the maze

You are provided with a complete labyrinth, like a 2-dimensional grid, more specifically in your language: an array of strings.

maze[0][0] is the upper left corner maze[maze[maze.length - 1][maze[0].length - 1] is the lower right corner

Within this 2D grid:

' ' Free space you can walk through
'#' bush with thorns (You can't walk through it)
'^', '<', 'v' or '>' Your body facing to north, west, south or east, respectively.

Enter fullscreen mode Exit fullscreen mode

Original Kata: Escape the maze

Note on Kata

You are given a series of mazes, your position and you must provide an array of moves to get out. It’s incredibly entertaining!

[ '##########',
  '# #',
  '# ##### #',
  '# # # #',
  '# #^# # #',
  '# ### # #',
  '# # #',
  '######## #' ]

Enter fullscreen mode Exit fullscreen mode

Katas with honorable mention

There are other Katas that I like very much but they were left out of this top. Check them out.

Top comments (0)