DEV Community

protium
protium

Posted on • Updated on

Coding Problems, TDD, and CI

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.

it("should return false for an invalid BST");
Enter fullscreen mode Exit fullscreen mode

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.

monorepo setup

Ideally a monorepo (see definition) 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
  • go: go test
  • python: python -m unittest
  • rust: cargo test
  • typescript: jest

Coding Problems

Most of the problems listed in the repo come from leetcode. It's a great resource to practice your problem solving skills. Another platform I like is hackerrank.

Playing around: compare the runtime for solutions in different languages in leetcode. For this problem I got this numbers for C++ and Rust
wow

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 logo protiumx / algo

Coding problems with modern languages, TDD and CI

algo

Coding problems with modern languages, TDD and CI.

Dependencies

  • Cargo
  • CMake
  • Go
  • Python3
  • NodeJS, Yarn

Folder structure

  • packages/
    • algo-cpp/: solutions in c++ (17)
    • algo-go/: solutions in golang
    • algo-py/: solutions in python 3
    • algo-rust/: solutions in rust
    • algo-ts/: solutions in typescript

Packages

algo-cpp

algo-go

algo-py

algo-rust

  • rustc 1.56
  • Testing: cargo test
  • Conventions: cargo clippy

algo-ts

Coding Problems

All problems are prefixed with algo- an enumerated from zero. This way you can easily find the solutions in any package Checkout the list of problems here

Testing

All packages are configured to use Makefile as follow

make -C packages/algo-[lang]/ test
Enter fullscreen mode Exit fullscreen mode

Except for algo-ts, where we use yarn

yarn --cwd packages/algo-ts/ 
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)