Coding Problems, TDD, and CI
Tags: algorithms, go, motivation, programming
Published: 2021-12-11 20:12:55

Hi everyone. In this post I want to share a little project I've been working on in the past few days. Let's dive into it

## algo

**algo** is short for **algorithms**. **algo** is a set of **coding problems** where the solutions are written following conventions and **TDD** practices in **modern languages**. Modern languages, in my humble opinion, are those that are being requested everywhere in the industry at the moment, these are: **c++**, **go**, **python**, **rust**, and **typescript**. 

The project is set up as **monorepo** were each package name has the prefix `algo-`.
When setting up a monorepo you will always need to choose a **build system**, commonly a CLI tool that can run commands for any of your packages. In this case I chose to work with `make`.

## TDD

For all the coding problems I followed TDD/BDD practices (I prefer the last). This is also how I solve problems during coding interviews. It's not necessary to always start with a failing test but it should be a common practice to use your test to describe the expected **behaviour** of your algorithm.

E.g.

```js
it("should return false for an invalid BST");
```

## CI

I consider CI a must for any of my projects. **Continuos Integration** will always ensure that your main branch can build with no errors, thus any branch you want to squash/rebase into **main** should have a green CI check. 

Setting up CI for all your projects, no matter the size, is always a good practice.

For this project CI is very simple: run linting (currently this is a TODO) and run **tests**. If all tests have passed with no errors, then the CI check is **green**.
You can check my github workflows [here](https://github.com/protiumx/algo/tree/main/.github/workflows).

## monorepo setup

Ideally a **monorepo** ([see definition](https://www.perforce.com/blog/vcs/what-monorepo)) would contain different packages written with the **same** language, which will make it easier to share common code/types/config/dependencies. In this case I chose a monorepo structure to add packages written using different languages which also have different build systems. To make it simple, I have added a `Makefile` to each package (except for the typescript package, where I use yarn), thus consolidating the build system.

## Testing

For each package I'm using the standard testing tools:

- `C++`: [gtest](https://github.com/google/googletest) 
- `go`: `go test`
- `python`: `python -m unittest`
- `rust`: `cargo test`
- `typescript`: [jest](https://jestjs.io/)

## Coding Problems

Most of the problems listed in the repo come from [leetcode](https://leetcode.com/). It's a great resource to practice your problem solving skills. Another platform I like is [hackerrank](https://hackerrank.com).

Playing around: compare the runtime for solutions in different languages in leetcode. For this [problem](https://leetcode.com/problems/minimum-path-sum) I got this numbers for C++ and Rust
![wow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hy12kgfvs7nmvun600k4.png)
 

## Contributing

I'd love to see some of you contributing with either adding more problems or improving the existing solutions (text and code). There is also a TODO list in the repo's README that needs some love.


{% github protiumx/algo %}


That's all. I hope you can find something useful on this post and project.
Thanks :robot: