Giter Site home page Giter Site logo

kaiosilveira / hacker-rank-challenges Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 2.16 MB

An aggregator of my completed code challenges in Hacker Rank, containing detailed explanation, benchmarking, time complexity analysis, and thorough testing

Ruby 100.00%
ruby hacker-rank-solutions time-complexity-analysis time-complexity-visualization code-challenges

hacker-rank-challenges's Introduction

Continuous Integration

Problem-solving

This repository is an aggregator of my solutions to code challenges. Inside each solution you will find:

  • a main file for the challenge, which contains the samples and the challenge execution (index.rb)
  • the challenge wrapper, with constraint validations (challenge.rb + challenge.spec.rb)
  • the underlying algorithm used to solve the challenge (algorithm.rb + algorithm.spec.rb)
  • a benchmark of the results (benchmarking.rb + some CSVs with benchmark output)
  • a README.md file with a detailed explanation of the code implemented

Programming language choice ๐Ÿ‘จ๐Ÿฝโ€๐Ÿ’ป

Because of its simplicity, elegance and API completeness, Ruby was the chosen programming language to implement the code for the challenges.

Available tasks โš™๏ธ

  • rake test: run all unit tests from all files
  • rake create_challenge'[challenge_name]': create a new directory with the default structure for a challenge

Testing & Continuous integration ๐Ÿงช

Each challenge contains a unit test suite covering the constraints described by the challenge itself, at least one happy path, and sometimes some interesting particular cases.

The built-in test/unit test framework was used to implement the test suites, as no external dependency will be needed.

Continuous integration

A simple CI pipeline was put in place to execute the tests for each directory inside this repo. It sets up Ruby and runs the tests. This happens for every push to main. The config is:

name: Continuous Integration

on:
  push:
    branches: ["main"]

jobs:
  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        ruby-version: ["2.6"]

    steps:
      - uses: actions/checkout@v3
      - name: Set up Ruby
        uses: ruby/setup-ruby@v1
        with:
          ruby-version: ${{ matrix.ruby-version }}
          bundler: "Gemfile.lock"
          bundler-cache: true
      - name: Lock bundle
        run: bundle lock --add-platform x86_64-linux

      - name: Run tests
        run: bundle exec rake

See ruby.yml for the actual file.

Benchmarking โฐ

To benchmark the code execution, the benchmark gem was used. A benchmarking.rb module was created to abstract the setup for the benchmarking, allowing the client code to call it passing a reference to the function to be executed and some more configuration parameters. The Benchmarking module also aggregates the value of n and the time it took to run the function for each execution and gives it back to the client code, so it can store the results and perform the graphical analysis later. See below an example of the benchmark in action:

โžœ ruby ./left-rotation/benchmarking.rb
n = 0  0.000011   0.000003   0.000014 (0.000011)
n = 1  0.000003   0.000001   0.000004 (0.000003)
n = 2  0.000004   0.000001   0.000005 (0.000005)
n = 3  0.000003   0.000000   0.000003 (0.000004)
n = 4  0.000003   0.000000   0.000003 (0.000003)
n = 5  0.000003   0.000001   0.000004 (0.000003)
n = 6  0.000005   0.000001   0.000006 (0.000005)
n = 7  0.000003   0.000000   0.000003 (0.000004)
n = 8  0.000004   0.000002   0.000006 (0.000004)
n = 9  0.000003   0.000001   0.000004 (0.000003)
n = 10  0.000005   0.000001   0.000006 (0.000005)

Time complexity ๐Ÿ“ˆ

The time complexity is calculated for each challenge, using the Big O notation. Common time complexities are:

  • Constant: $O(1)$
  • Linear: $O(n)$
  • Quadratic: $O(n^2)$

The experiment to calculate the time complexity for each challenge is an exercise of using all possible values described in the Constraints section of the challenge at Hacker Rank, always with an eye on the term that varies the most and could cause the biggest impact on the code execution. The analysis is performed on the dump generated by the benchmark process described above.

For each challenge, a chart was created to allow us to visually identify the time complexity for the solution. The YouPlot gem was used to create the charts from the command line. See below some examples of generated charts:

  • Constant time complexity $O(1)$:
โžœ cat ./cat-and-mouse/results.csv | uplot line -d, -w 50 -h 15 -t Results --canvas ascii --xlabel n --ylabel "T(n)"
                                   Results
             โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        0.02 โ”‚                                                  โ”‚
             โ”‚                                                  โ”‚
             โ”‚                                                  โ”‚
             โ”‚                                                  โ”‚
             โ”‚                                                  โ”‚
             โ”‚                                                  โ”‚
             โ”‚                                                  โ”‚
   T(n)      โ”‚                                   |              โ”‚
             โ”‚                          .        |              โ”‚
             โ”‚                          |        |              โ”‚
             โ”‚                          |    .   |              โ”‚
             โ”‚                          |    |   |              โ”‚
             โ”‚ .        ,.              |    |   |              โ”‚
             โ”‚ | .    , ||              |    |   |              โ”‚
           0 โ”‚@@_11___]_L1__1___________]____]___L_________]____โ”‚
             โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
             0                                            1000000
                                      n
  • Linear time complexity $O(n)$:
โžœ cat ./left-rotation/results.csv | uplot line -d, -w 50 -h 15 -t Results --canvas ascii --xlabel n --ylabel "T(n)"
                                   Results
             โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        0.02 โ”‚                                                  โ”‚
             โ”‚                                                  โ”‚
             โ”‚                                                  โ”‚
             โ”‚                                                  โ”‚
             โ”‚                                                , โ”‚
             โ”‚                                                /\โ”‚
             โ”‚                                               . |โ”‚
   T(n)      โ”‚                                         .r./""`  โ”‚
             โ”‚                                     __/"`        โ”‚
             โ”‚                            ._.--"`-/             โ”‚
             โ”‚                        _.--`                     โ”‚
             โ”‚                   r/"""                          โ”‚
             โ”‚             .r""/"                               โ”‚
             โ”‚     _.__--""`                                    โ”‚
           0 โ”‚__--" `                                           โ”‚
             โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
             0                                             100000
                                      n
  • Quadratic time complexity $O(n^{2})$:
โžœ cat ./electronic-shops/results.csv | uplot line -d, -w 50 -h 15 -t Results --canvas ascii --xlabel n --ylabel "T(n)"
                                Results
          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
        5 โ”‚                                            ./    โ”‚
          โ”‚                                          .r`     โ”‚
          โ”‚                                         .`       โ”‚
          โ”‚                                       ./         โ”‚
          โ”‚                                     ./           โ”‚
          โ”‚                                   ./`            โ”‚
          โ”‚                                 ./`              โ”‚
   T(n)   โ”‚                              .r"                 โ”‚
          โ”‚                            ./`                   โ”‚
          โ”‚                          ./`                     โ”‚
          โ”‚                       _-"                        โ”‚
          โ”‚                    _-"                           โ”‚
          โ”‚                _r/"                              โ”‚
          โ”‚          ._r-/"                                  โ”‚
        0 โ”‚_____.--/"`                                       โ”‚
          โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
          0                                              10000

hacker-rank-challenges's People

Contributors

kaiosilveira avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

hacker-rank-challenges's Issues

Fix Rubocop warnings

This codebase was initially structured without linting, so after setting up Rubocop, a lot of warnings were shown.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.