Post/Code

HomeAboutUsesNow

Daily Coding Problems.

I recently subscribed to the Daily Coding Problems email newsletter. As they put it:

Get exceptionally good at coding interviews by solving one problem every day.

Now, I am not planning on interviewing any time soon, however, I feel that algorithms and mathematical-type programming is something I need to improve on. As a result, I thought it would be a good idea to take up the challenge!

From the get go, I am a little behind. I have already received 7 emails, but I only go to doing the first one today. So much for New Year's Resolutions!

I liken these coding problems to fitness drills in sport - while a sportsman or woman may be fit and be good at the sport they play without specific fitness drills of training. Chances are very good that their overall game will be much improved by doing specific drills and training that focus on individual elements of their game. This is my development version of the daily jog or push-ups. Short, limited exercises that don't take too much time, but are focused and when done repeatedly will lead to health and strength improvements!

Tests.

As part of this exercise, I am also including tests.

  • Tests are essential to modern development.
  • Tests provide an easy way to run the code.
  • I want to get more familiar with writing tests before I write code.

For the tests, I am using Jest. Namely because Jest is easy to setup and get going and I have familiarity with Jest from using it with Node.js and Nest.js.

First Problem.

It is only in the past few years of my career that I have started to break out of the negative thought pattern that I wasn't a real developer. Part of that pattern remains and is somewhat rooted in the fact that while I write code that works. I am never quite sure if the code I write is good. Good is subjective in a sense, but from my perspective I mean good in the sense that:

  1. The code is not terrible
  2. The code follows good practice and is up to date with modern methods.
  3. The code is sensible and readable.

For example, doing this first problem some thoughts that go through my head are:

  • I know using for loops is not trendy and does not follow functional programming. As such, I started out using .map(). This then revealed a flaw in my thinking because I did not want to return a new array, I only wanted to iterate over the numbers and add them.
  • From that thinking, .map was not suitable because it returns a new array and I did not want a new array.
  • Similarly, .filter and .reduce were not suitable either.
  • I considered forEach, however, I remembered reading somewhere that ultimately, for loops were still more performant than forEach.
  • I had the emails suggested 60 minute timeline in mind, as such, decided to solve on the spot so I went for the trusty old for loop!

In any event, much of what I have written above might be wrong and as a result, I have some homework to do! Namely to find out:

  • Is a for loop more performant than forEach?
  • Is there a more modern ES6 construct that only iterates without return a new collection?
  • I want to write a solution using modern ES6

The repo is here.