From 6c4c5238db77b773abcee7ea3f4871294badeed1 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Mon, 19 Oct 2026 21:18:33 -0300 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation=20Kit?= =?UTF-8?q?:=20Arrays:=20Left=20Rotation.=20Solved=20=E2=9C=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../arrays/ctci_array_left_rotation.md | 65 +++++++++++++++++++ .../arrays/ctci_array_left_rotation.hpp | 11 ++++ .../arrays/ctci_array_left_rotation.cpp | 27 ++++++++ .../arrays/ctci_array_left_rotation.test.cpp | 32 +++++++++ .../ctci_array_left_rotation.testcases.json | 56 ++++++++++++++++ 5 files changed, 191 insertions(+) create mode 100644 docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md create mode 100644 src/lib/exercises/include/exercises/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.hpp create mode 100644 src/lib/exercises/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.cpp create mode 100644 src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.cpp create mode 100644 src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json diff --git a/docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md b/docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md new file mode 100644 index 0000000..4f5c0e9 --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md @@ -0,0 +1,65 @@ +# [Arrays: Left Rotation](https://www.hackerrank.com/challenges/ctci-array-left-rotation) + +Given an array and a number, d, perform d left rotations on the array. + +- Difficulty: `#easy` +- Category: `#ProblemSolvingBasic` `#arrays` + +A left rotation operation on an array shifts each of the array's elements +$ 1 $ unit to the left. For example, if $ 2 $ left rotations are performed +on array $ [1, 2, 3, 4, 5] $, then the array would become $ [3, 4, 5, 1, 2] $. +Note that the lowest index item moves to the highest index in a rotation. +This is called a circular array. + +Given an array $ a $ of $ n $ integers and a number, $ d $, perform $ d $ left +rotations on the array. Return the updated array to be printed as a single +line of space-separated integers. + +## Function Description + +Complete the function rotLeft in the editor below. + +rotLeft has the following parameter(s): + +- int a[n]: the array to rotate +- int d: the number of rotations + +## Returns + +- int a'[n]: the rotated array + +## Input Format + +The first line contains two space-separated integers $ n $ and $ d $, the size +of $ a $ and the number of left rotations. +The second line contains $ n $ space-separated integers, each an $ a[i] $. + +## Constraints + +- $ 1 \leq n \leq 10^5 $ +- $ 1 \leq d \leq n $ +- $ 1 \leq a[i] \leq 10^6 $ + +## Sample Input + +```text +5 4 +1 2 3 4 5 +``` + +## Sample Output + +```text +5 1 2 3 4 +``` + +## Explanation + +When we perform $ d = 4 $ left rotations, the array undergoes the following +sequence of changes: + +> [1, 2, 3, 4, 5] +> -> [2, 3, 4, 5, 1] +> -> [3, 4, 5, 1, 2] +> -> [4, 5, 1, 2, 3] +> -> [5, 1, 2, 3, 4] diff --git a/src/lib/exercises/include/exercises/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.hpp b/src/lib/exercises/include/exercises/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.hpp new file mode 100644 index 0000000..c7ab0cb --- /dev/null +++ b/src/lib/exercises/include/exercises/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +using std::vector; + +namespace hackerrank::interview_preparation_kit::arrays { + +vector rotLeft(vector a, int d); + +} // namespace hackerrank::interview_preparation_kit::arrays diff --git a/src/lib/exercises/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.cpp b/src/lib/exercises/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.cpp new file mode 100644 index 0000000..26e6501 --- /dev/null +++ b/src/lib/exercises/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.cpp @@ -0,0 +1,27 @@ +#include + +#include + +/** + * @link Problem definition + * [[docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md]] + */ + +namespace hackerrank::interview_preparation_kit::arrays { + +// cppcheck-suppress passedByValue +vector rotLeft(vector a, int d) { + size_t a_count = a.size(); + std::vector result(a_count, 0); + + int rotations = d % a.size(); + + for (int i = 0; i < a_count; ++i) { + size_t new_position = (i + a_count - rotations) % a_count; + result[new_position] = a[i]; + } + + return result; +} + +} // namespace hackerrank::interview_preparation_kit::arrays diff --git a/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.cpp b/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.cpp new file mode 100644 index 0000000..22bc182 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.cpp @@ -0,0 +1,32 @@ +// 3rd party libs +#include +#include + +// local libs +#include + +// std libs +#include +#include +#include + +using json = nlohmann::json; + +TEST_CASE("rotLeft JSON Test Cases", "[jsontestcase] [hackerrank] [arrays]") { + SECTION("rotLeft: Normal workflow") { + std::filesystem::path path = std::filesystem::path(__FILE__).parent_path() / + "ctci_array_left_rotation.testcases.json"; + + INFO("rotLeft JSON test cases FILE: " << path.string()); + + std::ifstream f(path); + json data = json::parse(f); + + for (const auto &testcase : data) { + const vector result = + hackerrank::interview_preparation_kit::arrays::rotLeft( + testcase["input"], testcase["d_rotations"]); + CHECK(result == testcase["expected"]); + } + } +} diff --git a/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json b/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json new file mode 100644 index 0000000..7a411a2 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json @@ -0,0 +1,56 @@ +[ + { + "title": "Own 0", + "input": [1, 2, 3, 4, 5], + "d_rotations": 1, + "expected": [2, 3, 4, 5, 1] + }, + { + "title": "Own 1", + "input": [2, 3, 4, 5, 1], + "d_rotations": 1, + "expected": [3, 4, 5, 1, 2] + }, + { + "title": "Own 2", + "input": [3, 4, 5, 1, 2], + "d_rotations": 1, + "expected": [4, 5, 1, 2, 3] + }, + { + "title": "Own 3", + "input": [4, 5, 1, 2, 3], + "d_rotations": 1, + "expected": [5, 1, 2, 3, 4] + }, + { + "title": "Own 4", + "input": [5, 1, 2, 3, 4], + "d_rotations": 1, + "expected": [1, 2, 3, 4, 5] + }, + { + "title": "Sample Test case 0", + "input": [1, 2, 3, 4, 5], + "d_rotations": 4, + "expected": [5, 1, 2, 3, 4] + }, + { + "title": "Sample Test case 1", + "input": [ + 41, 73, 89, 7, 10, 1, 59, 58, 84, 77, 77, 97, 58, 1, 86, 58, 26, 10, 86, + 51 + ], + "d_rotations": 10, + "expected": [ + 77, 97, 58, 1, 86, 58, 26, 10, 86, 51, 41, 73, 89, 7, 10, 1, 59, 58, 84, + 77 + ] + }, + { + "title": "Sample Test case 2", + "input": [33, 47, 70, 37, 8, 53, 13, 93, 71, 72, 51, 100, 60, 87, 97], + "d_rotations": 13, + "expected": [87, 97, 33, 47, 70, 37, 8, 53, 13, 93, 71, 72, 51, 100, 60] + } +]