{"id":22496689,"url":"https://github.com/kaiosilveira/hacker-rank-challenges","last_synced_at":"2025-03-27T21:24:17.514Z","repository":{"id":57758327,"uuid":"527061366","full_name":"kaiosilveira/hacker-rank-challenges","owner":"kaiosilveira","description":"An aggregator of my completed code challenges in Hacker Rank, containing detailed explanation, benchmarking, time complexity analysis, and thorough testing","archived":false,"fork":false,"pushed_at":"2023-11-30T13:34:06.000Z","size":2210,"stargazers_count":2,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-01T23:44:33.888Z","etag":null,"topics":["code-challenges","hacker-rank-solutions","ruby","time-complexity-analysis","time-complexity-visualization"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kaiosilveira.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null},"funding":{"github":["kaiosilveira"]}},"created_at":"2022-08-20T23:22:26.000Z","updated_at":"2023-11-30T14:09:14.000Z","dependencies_parsed_at":"2023-11-30T14:25:14.811Z","dependency_job_id":"faca08e9-507c-485a-9d61-7366a0cff14e","html_url":"https://github.com/kaiosilveira/hacker-rank-challenges","commit_stats":null,"previous_names":["kaiosilveira/problem-solving"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaiosilveira%2Fhacker-rank-challenges","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaiosilveira%2Fhacker-rank-challenges/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaiosilveira%2Fhacker-rank-challenges/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kaiosilveira%2Fhacker-rank-challenges/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kaiosilveira","download_url":"https://codeload.github.com/kaiosilveira/hacker-rank-challenges/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245925440,"owners_count":20694900,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["code-challenges","hacker-rank-solutions","ruby","time-complexity-analysis","time-complexity-visualization"],"created_at":"2024-12-06T20:13:41.815Z","updated_at":"2025-03-27T21:24:17.489Z","avatar_url":"https://github.com/kaiosilveira.png","language":"Ruby","funding_links":["https://github.com/sponsors/kaiosilveira"],"categories":[],"sub_categories":[],"readme":"[![Continuous Integration](https://github.com/kaiosilveira/hacker-rank-challenges/actions/workflows/ruby.yml/badge.svg)](https://github.com/kaiosilveira/hacker-rank-challenges/actions/workflows/ruby.yml)\n\n# Problem-solving\n\nThis repository is an aggregator of my solutions to code challenges. Inside each solution you will find:\n\n- a main file for the challenge, which contains the samples and the challenge execution (`index.rb`)\n- the challenge wrapper, with constraint validations (`challenge.rb` + `challenge.spec.rb`)\n- the underlying algorithm used to solve the challenge (`algorithm.rb` + `algorithm.spec.rb`)\n- a benchmark of the results (`benchmarking.rb` + some CSVs with benchmark output)\n- a `README.md` file with a detailed explanation of the code implemented\n\n## Programming language choice 👨🏽‍💻\n\nBecause of its simplicity, elegance and API completeness, Ruby was the chosen programming language to implement the code for the challenges.\n\n### Available tasks ⚙️\n\n- `rake test`: run all unit tests from all files\n- `rake create_challenge'[challenge_name]'`: create a new directory with the default structure for a challenge\n\n## Testing \u0026 Continuous integration 🧪\n\nEach 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.\n\nThe built-in `test/unit` test framework was used to implement the test suites, as no external dependency will be needed.\n\n**Continuous integration**\n\nA 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:\n\n```yml\nname: Continuous Integration\n\non:\n  push:\n    branches: [\"main\"]\n\njobs:\n  test:\n    runs-on: ubuntu-latest\n    strategy:\n      matrix:\n        ruby-version: [\"2.6\"]\n\n    steps:\n      - uses: actions/checkout@v3\n      - name: Set up Ruby\n        uses: ruby/setup-ruby@v1\n        with:\n          ruby-version: ${{ matrix.ruby-version }}\n          bundler: \"Gemfile.lock\"\n          bundler-cache: true\n      - name: Lock bundle\n        run: bundle lock --add-platform x86_64-linux\n\n      - name: Run tests\n        run: bundle exec rake\n```\n\nSee [ruby.yml](./.github/workflows/ruby.yml) for the actual file.\n\n## Benchmarking ⏰\n\nTo benchmark the code execution, the [benchmark](https://github.com/ruby/benchmark) gem was used. A [benchmarking.rb](./_utils/benchmark.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:\n\n```console\n➜ ruby ./left-rotation/benchmarking.rb\nn = 0  0.000011   0.000003   0.000014 (0.000011)\nn = 1  0.000003   0.000001   0.000004 (0.000003)\nn = 2  0.000004   0.000001   0.000005 (0.000005)\nn = 3  0.000003   0.000000   0.000003 (0.000004)\nn = 4  0.000003   0.000000   0.000003 (0.000003)\nn = 5  0.000003   0.000001   0.000004 (0.000003)\nn = 6  0.000005   0.000001   0.000006 (0.000005)\nn = 7  0.000003   0.000000   0.000003 (0.000004)\nn = 8  0.000004   0.000002   0.000006 (0.000004)\nn = 9  0.000003   0.000001   0.000004 (0.000003)\nn = 10  0.000005   0.000001   0.000006 (0.000005)\n```\n\n## Time complexity 📈\n\nThe time complexity is calculated for each challenge, using the [Big O](https://en.wikipedia.org/wiki/Big_O_notation) notation. Common time complexities are:\n\n- Constant: $O(1)$\n- Linear: $O(n)$\n- Quadratic: $O(n^2)$\n\nThe 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.\n\nFor each challenge, a chart was created to allow us to visually identify the time complexity for the solution. The [YouPlot](https://github.com/red-data-tools/YouPlot) gem was used to create the charts from the command line. See below some examples of generated charts:\n\n- Constant time complexity $O(1)$:\n\n```console\n➜ cat ./cat-and-mouse/results.csv | uplot line -d, -w 50 -h 15 -t Results --canvas ascii --xlabel n --ylabel \"T(n)\"\n                                   Results\n             ┌──────────────────────────────────────────────────┐\n        0.02 │                                                  │\n             │                                                  │\n             │                                                  │\n             │                                                  │\n             │                                                  │\n             │                                                  │\n             │                                                  │\n   T(n)      │                                   |              │\n             │                          .        |              │\n             │                          |        |              │\n             │                          |    .   |              │\n             │                          |    |   |              │\n             │ .        ,.              |    |   |              │\n             │ | .    , ||              |    |   |              │\n           0 │@@_11___]_L1__1___________]____]___L_________]____│\n             └──────────────────────────────────────────────────┘\n             0                                            1000000\n                                      n\n```\n\n- Linear time complexity $O(n)$:\n\n```console\n➜ cat ./left-rotation/results.csv | uplot line -d, -w 50 -h 15 -t Results --canvas ascii --xlabel n --ylabel \"T(n)\"\n                                   Results\n             ┌──────────────────────────────────────────────────┐\n        0.02 │                                                  │\n             │                                                  │\n             │                                                  │\n             │                                                  │\n             │                                                , │\n             │                                                /\\│\n             │                                               . |│\n   T(n)      │                                         .r./\"\"`  │\n             │                                     __/\"`        │\n             │                            ._.--\"`-/             │\n             │                        _.--`                     │\n             │                   r/\"\"\"                          │\n             │             .r\"\"/\"                               │\n             │     _.__--\"\"`                                    │\n           0 │__--\" `                                           │\n             └──────────────────────────────────────────────────┘\n             0                                             100000\n                                      n\n\n```\n\n- Quadratic time complexity $O(n^{2})$:\n\n```console\n➜ cat ./electronic-shops/results.csv | uplot line -d, -w 50 -h 15 -t Results --canvas ascii --xlabel n --ylabel \"T(n)\"\n                                Results\n          ┌──────────────────────────────────────────────────┐\n        5 │                                            ./    │\n          │                                          .r`     │\n          │                                         .`       │\n          │                                       ./         │\n          │                                     ./           │\n          │                                   ./`            │\n          │                                 ./`              │\n   T(n)   │                              .r\"                 │\n          │                            ./`                   │\n          │                          ./`                     │\n          │                       _-\"                        │\n          │                    _-\"                           │\n          │                _r/\"                              │\n          │          ._r-/\"                                  │\n        0 │_____.--/\"`                                       │\n          └──────────────────────────────────────────────────┘\n          0                                              10000\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaiosilveira%2Fhacker-rank-challenges","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkaiosilveira%2Fhacker-rank-challenges","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkaiosilveira%2Fhacker-rank-challenges/lists"}