From 52d4efaf877ea4509bb3150aab50d65b943409d2 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Mon, 19 Oct 2026 14:32:48 -0300 Subject: [PATCH 1/3] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation?= =?UTF-8?q?=20Kit:=20Array:=202D=20Array=20-=20DS.=20Solved=20=E2=9C=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../arrays/2d_arrays.md | 135 ++++++++++++++++++ .../arrays/2d_arrays.hpp | 11 ++ .../arrays/2d_arrays.cpp | 33 +++++ .../arrays/2d_arrays.test.cpp | 39 +++++ .../arrays/2d_arrays.testcases.json | 38 +++++ 5 files changed, 256 insertions(+) create mode 100644 docs/hackerrank/interview_preparation_kit/arrays/2d_arrays.md create mode 100644 src/lib/exercises/include/exercises/hackerrank/interview_preparation_kit/arrays/2d_arrays.hpp create mode 100644 src/lib/exercises/src/hackerrank/interview_preparation_kit/arrays/2d_arrays.cpp create mode 100644 src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/2d_arrays.test.cpp create mode 100644 src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/2d_arrays.testcases.json diff --git a/docs/hackerrank/interview_preparation_kit/arrays/2d_arrays.md b/docs/hackerrank/interview_preparation_kit/arrays/2d_arrays.md new file mode 100644 index 0000000..9c92ffe --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/arrays/2d_arrays.md @@ -0,0 +1,135 @@ +# [Arrays: 2D Array - DS](https://www.hackerrank.com/challenges/2d-array) + +- Difficulty: ` #easy ` +- Category: ` #ProblemSolvingBasic ` + +Given a 6 × 6 2D Array, `arr`: + +```text +1 1 1 0 0 0 +0 1 0 0 0 0 +1 1 1 0 0 0 +0 0 0 0 0 0 +0 0 0 0 0 0 +0 0 0 0 0 0 +``` + +An hourglass in `A` is a subset of values with indices falling in this pattern + in `arr`'s graphical representation: + +```text +a b c + d +e f g +``` + +There are `16` hourglasses in `arr`. +An hourglass sum is the sum of an hourglass' values. +Calculate the hourglass sum for every hourglass in `arr`, +then print the maximum hourglass sum. The array will always be 6 × 6. + +## Example + +arr = + +```text +-9 -9 -9 1 1 1 + 0 -9 0 4 3 2 +-9 -9 -9 1 2 3 + 0 0 8 6 6 0 + 0 0 0 -2 0 0 + 0 0 1 2 4 0 +``` + +The `16` hourglass sums are: + +```text +-63, -34, -9, 12, +-10, 0, 28, 23, +-27, -11, -2, 10, + 9, 17, 25, 18 +``` + +The highest hourglass sum is `26` from the hourglass beginning +at row `1`, column `2`: + +```text +0 4 3 + 1 +8 6 6 +```` + +**Note**: If you have already solved the Java domain's Java 2D Array challenge, +you may wish to skip this challenge. + +## Function Description + +Complete the function hourglassSum in the editor below. + +hourglassSum has the following parameter(s): + +- `int arr[6][6]`: an array of integers + +## Returns + +- int: the maximum hourglass sum + +## Input Format + +Each of the `6` lines of inputs `arr[i]` contains space-separated integers `arr[i][j]`. + +## Constraints + +- $9 \leq arr[i][j] \leq 9$ +- $0 \leq i, j \leq 5$ + +## Output Format + +Print the largest (maximum) hourglass sum found in `arr`. + +## Sample Input + +```text +1 1 1 0 0 0 +0 1 0 0 0 0 +1 1 1 0 0 0 +0 0 2 4 4 0 +0 0 0 2 0 0 +0 0 1 2 4 0 +``` + +## Sample Output + +```text +19 +``` + +## Explanation + +`arr` contains the following hourglasses: + +```text +111 110 100 000 + 1 0 0 0 +111 110 100 000 + +010 100 000 000 + 0 1 0 0 +002 024 244 440 + +111 110 100 000 + 0 2 4 4 +000 002 020 200 + +002 024 244 440 + 0 0 2 0 +001 012 124 240 +``` + +The hourglass with the maximum sum (`19`) is: + +```text +2 4 4 + 2 +1 2 4 +``` diff --git a/src/lib/exercises/include/exercises/hackerrank/interview_preparation_kit/arrays/2d_arrays.hpp b/src/lib/exercises/include/exercises/hackerrank/interview_preparation_kit/arrays/2d_arrays.hpp new file mode 100644 index 0000000..e367469 --- /dev/null +++ b/src/lib/exercises/include/exercises/hackerrank/interview_preparation_kit/arrays/2d_arrays.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include + +using std::vector; + +namespace hackerrank::interview_preparation_kit::arrays { + +int hourglassSum(vector> arr); + +} diff --git a/src/lib/exercises/src/hackerrank/interview_preparation_kit/arrays/2d_arrays.cpp b/src/lib/exercises/src/hackerrank/interview_preparation_kit/arrays/2d_arrays.cpp new file mode 100644 index 0000000..6f035b7 --- /dev/null +++ b/src/lib/exercises/src/hackerrank/interview_preparation_kit/arrays/2d_arrays.cpp @@ -0,0 +1,33 @@ + +#include + +#include +using std::vector; + +/** + * @link Problem definition + * [[docs/hackerrank/interview_preparation_kit/arrays/2d_arrays.md]] + */ + +namespace hackerrank::interview_preparation_kit::arrays { + +int hourglassSum(vector> arr) { + int max_sum = -63; // Minimum possible hourglass sum (-9 * 7) + for (int i = 0; i <= arr.size() - 3; i++) { + for (int j = 0; j <= arr[0].size() - 3; j++) { + int current_sum = + // top + arr[i][j] + arr[i][j + 1] + arr[i][j + 2] + + // middle + arr[i + 1][j + 1] + + // bottom + arr[i + 2][j] + arr[i + 2][j + 1] + arr[i + 2][j + 2]; + if (current_sum > max_sum) { + max_sum = current_sum; + } + } + } + return max_sum; +} + +} // namespace hackerrank::interview_preparation_kit::arrays diff --git a/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/2d_arrays.test.cpp b/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/2d_arrays.test.cpp new file mode 100644 index 0000000..27bb3aa --- /dev/null +++ b/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/2d_arrays.test.cpp @@ -0,0 +1,39 @@ +// 3rd party libs +#include +#include + +// local libs +#include + +// std libs +#include +#include +#include + +using json = nlohmann::json; + +TEST_CASE("hourglassSum JSON Test Cases", + "[hackerrank] [jsontestcase] [arrays]") { + std::filesystem::path path = std::filesystem::path(__FILE__).parent_path() / + "2d_arrays.testcases.json"; + + INFO("hourglassSum JSON test cases FILE: " << path.string()); + + std::ifstream f(path); + json data = json::parse(f); + + for (auto testcase : data) { + std::vector> matrix = testcase["input"]; + std::vector rows; + rows.reserve(matrix.size()); + + for (const auto &row : matrix) { + rows.push_back(const_cast(row.data())); + } + + long long result = + hackerrank::interview_preparation_kit::arrays::hourglassSum(matrix); + + CHECK(result == testcase["expected"]); + } +} diff --git a/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/2d_arrays.testcases.json b/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/2d_arrays.testcases.json new file mode 100644 index 0000000..91ecbd7 --- /dev/null +++ b/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/2d_arrays.testcases.json @@ -0,0 +1,38 @@ +[ + { + "title": "Sample Test Case 0", + "input": [ + [1, 1, 1, 0, 0, 0], + [0, 1, 0, 0, 0, 0], + [1, 1, 1, 0, 0, 0], + [0, 0, 2, 4, 4, 0], + [0, 0, 0, 2, 0, 0], + [0, 0, 1, 2, 4, 0] + ], + "expected": 19 + }, + { + "title": "Sample Test Case 1", + "input": [ + [1, 1, 1, 0, 0, 0], + [0, 1, 0, 0, 0, 0], + [1, 1, 1, 0, 0, 0], + [0, 9, 2, -4, -4, 0], + [0, 0, 0, -2, 0, 0], + [0, 0, -1, -2, -4, 0] + ], + "expected": 13 + }, + { + "title": "Sample Test Case 2", + "input": [ + [-9, -9, -9, 1, 1, 1], + [0, -9, 0, 4, 3, 2], + [-9, -9, -9, 1, 2, 3], + [0, 0, 8, 6, 6, 0], + [0, 0, 0, -2, 0, 0], + [0, 0, 1, 2, 4, 0] + ], + "expected": 28 + } +] From 3028c8e07611eaf0b534f997a5d2583bfa2fe957 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Mon, 19 Oct 2026 15:26:12 -0300 Subject: [PATCH 2/3] [Hacker Rank] Interview Preparation Kit: Array: 2D Array - DS. CppCheck recommendation applied. --- .../interview_preparation_kit/arrays/2d_arrays.test.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/2d_arrays.test.cpp b/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/2d_arrays.test.cpp index 27bb3aa..9ed10bc 100644 --- a/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/2d_arrays.test.cpp +++ b/src/tests/unit/lib/hackerrank/interview_preparation_kit/arrays/2d_arrays.test.cpp @@ -27,9 +27,9 @@ TEST_CASE("hourglassSum JSON Test Cases", std::vector rows; rows.reserve(matrix.size()); - for (const auto &row : matrix) { - rows.push_back(const_cast(row.data())); - } + std::transform( + matrix.begin(), matrix.end(), std::back_inserter(rows), + [](const auto &row) { return const_cast(row.data()); }); long long result = hackerrank::interview_preparation_kit::arrays::hourglassSum(matrix); From db16d344b24cc63b9b68947a312d767fdf962777 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Mon, 19 Oct 2026 16:14:41 -0300 Subject: [PATCH 3/3] fix: [Hacker Rank] Interview Preparation Kit: Array: 2D Array - DS. CppCheck warnign: performance: Function parameter 'arr' should be passed by const reference. [passedByValue] --- .../hackerrank/interview_preparation_kit/arrays/2d_arrays.hpp | 2 +- .../hackerrank/interview_preparation_kit/arrays/2d_arrays.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib/exercises/include/exercises/hackerrank/interview_preparation_kit/arrays/2d_arrays.hpp b/src/lib/exercises/include/exercises/hackerrank/interview_preparation_kit/arrays/2d_arrays.hpp index e367469..01ebb74 100644 --- a/src/lib/exercises/include/exercises/hackerrank/interview_preparation_kit/arrays/2d_arrays.hpp +++ b/src/lib/exercises/include/exercises/hackerrank/interview_preparation_kit/arrays/2d_arrays.hpp @@ -6,6 +6,6 @@ using std::vector; namespace hackerrank::interview_preparation_kit::arrays { -int hourglassSum(vector> arr); +int hourglassSum(const vector> &arr); } diff --git a/src/lib/exercises/src/hackerrank/interview_preparation_kit/arrays/2d_arrays.cpp b/src/lib/exercises/src/hackerrank/interview_preparation_kit/arrays/2d_arrays.cpp index 6f035b7..c1d030c 100644 --- a/src/lib/exercises/src/hackerrank/interview_preparation_kit/arrays/2d_arrays.cpp +++ b/src/lib/exercises/src/hackerrank/interview_preparation_kit/arrays/2d_arrays.cpp @@ -11,7 +11,7 @@ using std::vector; namespace hackerrank::interview_preparation_kit::arrays { -int hourglassSum(vector> arr) { +int hourglassSum(const vector> &arr) { int max_sum = -63; // Minimum possible hourglass sum (-9 * 7) for (int i = 0; i <= arr.size() - 3; i++) { for (int j = 0; j <= arr[0].size() - 3; j++) {