From 60a7ee326e09429424bbec629b180a520f4cde48 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Wed, 19 Feb 2025 00:16:05 -0300 Subject: [PATCH 01/10] [REFACTOR] [Hacker Rank] Interview Preparation Kit: Array: 2D Array - DS. Adjusted the interface to match what hackerrank expects. --- .../interview_preparation_kit/arrays/2d_array.test.ts | 4 ++-- src/hackerrank/interview_preparation_kit/arrays/2d_array.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/hackerrank/interview_preparation_kit/arrays/2d_array.test.ts b/src/hackerrank/interview_preparation_kit/arrays/2d_array.test.ts index b72689d..cc0e6a2 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/2d_array.test.ts +++ b/src/hackerrank/interview_preparation_kit/arrays/2d_array.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from '@jest/globals'; import { logger as console } from '../../../logger'; -import { hourglassSum } from './2d_array'; +import twoDarray from './2d_array'; import TEST_CASES from './2d_array.testcases_test.json'; describe('arrays: 2d Array hourglassSum', () => { @@ -9,7 +9,7 @@ describe('arrays: 2d Array hourglassSum', () => { expect.assertions(3); TEST_CASES.forEach((test) => { - const answer = hourglassSum(test.input); + const answer = twoDarray.hourglassSum(test.input); console.debug( `gethourGlass(${test.input.toString()}) solution found: ${answer}` diff --git a/src/hackerrank/interview_preparation_kit/arrays/2d_array.ts b/src/hackerrank/interview_preparation_kit/arrays/2d_array.ts index 333e404..c06fd9c 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/2d_array.ts +++ b/src/hackerrank/interview_preparation_kit/arrays/2d_array.ts @@ -2,7 +2,7 @@ * @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/2d_array.md]] */ -export function gethourGlass( +function gethourGlass( arr: number[][], positionX: number, positionY: number @@ -22,7 +22,7 @@ export function gethourGlass( return result; } -export function hourglassSum(arr: number[][]): number | null { +function hourglassSum(arr: number[][]): number | null { let matrixSize = 0; if (arr?.[0]) { From 6c1da15296cbf0829ddfc449406957f4e94d980a Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Thu, 20 Feb 2025 16:47:41 -0300 Subject: [PATCH 02/10] [REFACTOR] [Hacker Rank] Interview Preparation Kit: Arrays: Array Manipulation. Adjusted the interface to match what hackerrank expects. --- .../arrays/cruch_bruteforce.test.ts | 4 +- .../arrays/cruch_bruteforce.ts | 2 +- .../arrays/cruch_optimized.test.ts | 4 +- .../arrays/cruch_optimized.ts | 2 +- .../arrays/cruch_testcases_test.json | 60 ++++--------------- 5 files changed, 16 insertions(+), 56 deletions(-) diff --git a/src/hackerrank/interview_preparation_kit/arrays/cruch_bruteforce.test.ts b/src/hackerrank/interview_preparation_kit/arrays/cruch_bruteforce.test.ts index 33c27fe..2ccc494 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/cruch_bruteforce.test.ts +++ b/src/hackerrank/interview_preparation_kit/arrays/cruch_bruteforce.test.ts @@ -3,14 +3,14 @@ import { logger as console } from '../../../logger'; import TEST_CASES from './cruch_testcases_test.json'; -import { arrayManipulation } from './cruch_bruteforce'; +import crush from './cruch_bruteforce'; describe('arrays: crush (bruteforce) small cases', () => { it('arrayManipulation Test Cases', () => { expect.assertions(3); TEST_CASES.forEach((test) => { - const answer = arrayManipulation(test.n, test.queries); + const answer = crush.arrayManipulation(test.n, test.queries); console.debug( `arrayManipulation(${test.n}, ${test.queries.toString()}) solution found: ${answer}` diff --git a/src/hackerrank/interview_preparation_kit/arrays/cruch_bruteforce.ts b/src/hackerrank/interview_preparation_kit/arrays/cruch_bruteforce.ts index 4ebdf65..86fe2ba 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/cruch_bruteforce.ts +++ b/src/hackerrank/interview_preparation_kit/arrays/cruch_bruteforce.ts @@ -5,7 +5,7 @@ import { logger as console } from '../../../logger'; -export function arrayManipulation(n: number, queries: number[][]): number { +function arrayManipulation(n: number, queries: number[][]): number { const LENGTH = n + 1; const SURROGATE_VALUE = 0; const result: number[] = Array(LENGTH).fill(SURROGATE_VALUE); diff --git a/src/hackerrank/interview_preparation_kit/arrays/cruch_optimized.test.ts b/src/hackerrank/interview_preparation_kit/arrays/cruch_optimized.test.ts index da06999..b049136 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/cruch_optimized.test.ts +++ b/src/hackerrank/interview_preparation_kit/arrays/cruch_optimized.test.ts @@ -3,14 +3,14 @@ import { logger as console } from '../../../logger'; import TEST_CASES from './cruch_testcases_test.json'; -import { arrayManipulation } from './cruch_optimized'; +import crush from './cruch_optimized'; describe('arrays: crush (optimized)', () => { it('arrayManipulation Test Cases', () => { expect.assertions(3); TEST_CASES.forEach((test) => { - const answer = arrayManipulation(test.n, test.queries); + const answer = crush.arrayManipulation(test.n, test.queries); console.debug( `arrayManipulation(${test.n}, ${test.queries.toString()}) solution found: ${answer}` diff --git a/src/hackerrank/interview_preparation_kit/arrays/cruch_optimized.ts b/src/hackerrank/interview_preparation_kit/arrays/cruch_optimized.ts index a95f77d..ee8f7c8 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/cruch_optimized.ts +++ b/src/hackerrank/interview_preparation_kit/arrays/cruch_optimized.ts @@ -2,7 +2,7 @@ * @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/crush.md]] */ -export function arrayManipulation(n: number, queries: number[][]): number { +function arrayManipulation(n: number, queries: number[][]): number { // why adding 2? // first slot to adjust 1-based index and // last slot for storing accumSum result diff --git a/src/hackerrank/interview_preparation_kit/arrays/cruch_testcases_test.json b/src/hackerrank/interview_preparation_kit/arrays/cruch_testcases_test.json index ec7d19c..fa4f8a1 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/cruch_testcases_test.json +++ b/src/hackerrank/interview_preparation_kit/arrays/cruch_testcases_test.json @@ -3,21 +3,9 @@ "title": "Sample Test Case 0", "n": 5, "queries": [ - [ - 1, - 2, - 100 - ], - [ - 2, - 5, - 100 - ], - [ - 3, - 4, - 100 - ] + [1, 2, 100], + [2, 5, 100], + [3, 4, 100] ], "expected": 200 }, @@ -25,21 +13,9 @@ "title": "Sample Test Case 1", "n": 10, "queries": [ - [ - 1, - 5, - 3 - ], - [ - 4, - 8, - 7 - ], - [ - 6, - 9, - 1 - ] + [1, 5, 3], + [4, 8, 7], + [6, 9, 1] ], "expected": 10 }, @@ -47,26 +23,10 @@ "title": "Sample Test Case 3", "n": 10, "queries": [ - [ - 2, - 6, - 8 - ], - [ - 3, - 5, - 7 - ], - [ - 1, - 8, - 1 - ], - [ - 5, - 9, - 15 - ] + [2, 6, 8], + [3, 5, 7], + [1, 8, 1], + [5, 9, 15] ], "expected": 31 } From c4ea6d72269e140d8b34a68a438b32d55a1b29ad Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Thu, 20 Feb 2025 17:03:49 -0300 Subject: [PATCH 03/10] =?UTF-8?q?[REFACTOR]=20[Hacker=20Rank]=20Interview?= =?UTF-8?q?=20Preparation=20Kit:=20Arrays:=20Left=20Rotation.=20Solved=20?= =?UTF-8?q?=E2=9C=85.=20Adjusted=20the=20interface=20to=20match=20what=20h?= =?UTF-8?q?ackerrank=20expects.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../arrays/ctci_array_left_rotation.test.ts | 9 ++++++--- .../arrays/ctci_array_left_rotation.ts | 4 ++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts index b47a9fe..b2fa813 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts +++ b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from '@jest/globals'; import { logger as console } from '../../../logger'; -import { rotLeft, rotLeftOne } from './ctci_array_left_rotation'; +import arrayLeftRotation from './ctci_array_left_rotation'; import ROT_LEFT_ONE_TEST_CASES from './ctci_array_left_rotation.testcases.json'; @@ -11,7 +11,7 @@ describe('ctci_array_left_rotation', () => { ROT_LEFT_ONE_TEST_CASES.forEach((test) => { const input = test.numbers; - const answer = rotLeftOne(input); + const answer = arrayLeftRotation.rotLeftOne(input); console.debug( `rotLeftOne(${test.numbers.toString()}) solution found: ${answer.toString()}` @@ -29,7 +29,10 @@ describe('ctci_array_left_rotation', () => { ]; ROT_LEFT_TEST_CASES.forEach((value) => { - const answer = rotLeft(value.numbers, value.d_rotations); + const answer = arrayLeftRotation.rotLeft( + value.numbers, + value.d_rotations + ); console.debug( `rotLeft(${value.numbers.toString()}) solution found: ${answer.toString()}` diff --git a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.ts b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.ts index b60de67..6376d2e 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.ts +++ b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.ts @@ -2,7 +2,7 @@ * @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md]] */ -export function rotLeftOne(aNumbers: number[]): number[] { +function rotLeftOne(aNumbers: number[]): number[] { const first = aNumbers.shift(); if (first !== undefined) { aNumbers.push(first); @@ -11,7 +11,7 @@ export function rotLeftOne(aNumbers: number[]): number[] { return aNumbers; } -export function rotLeft(aNumbers: number[], dRotations: number): number[] { +function rotLeft(aNumbers: number[], dRotations: number): number[] { let output = [...aNumbers]; for (let i = 0; i < dRotations; i++) { From ccb6f768d68c548ca042e460b212291101a9ebb2 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Thu, 20 Feb 2025 23:57:11 -0300 Subject: [PATCH 04/10] [REFACTOR] [Hacker Rank] Interview Preparation Kit: Arrays: Left Rotation. New JSON data. --- .../arrays/ctci_array_left_rotation.test.ts | 33 +++++-------------- .../ctci_array_left_rotation.testcases.json | 13 +++++--- 2 files changed, 16 insertions(+), 30 deletions(-) diff --git a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts index b2fa813..6c629a2 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts +++ b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts @@ -3,42 +3,25 @@ import { logger as console } from '../../../logger'; import arrayLeftRotation from './ctci_array_left_rotation'; -import ROT_LEFT_ONE_TEST_CASES from './ctci_array_left_rotation.testcases.json'; +import ROT_LEFT_TEST_CASES from './ctci_array_left_rotation.testcases.json'; describe('ctci_array_left_rotation', () => { - it('rotLeftOne Test Cases', () => { - expect.assertions(5); - - ROT_LEFT_ONE_TEST_CASES.forEach((test) => { - const input = test.numbers; - const answer = arrayLeftRotation.rotLeftOne(input); - - console.debug( - `rotLeftOne(${test.numbers.toString()}) solution found: ${answer.toString()}` - ); - - expect(answer).toStrictEqual(test.expected); - }); - }); - it('rotLeft Test cases', () => { - expect.assertions(1); + expect.assertions(8); - const ROT_LEFT_TEST_CASES = [ - { numbers: [1, 2, 3, 4, 5], d_rotations: 4, expected: [5, 1, 2, 3, 4] } - ]; + ROT_LEFT_TEST_CASES.forEach((test) => { + const numbers = test.input as number[]; - ROT_LEFT_TEST_CASES.forEach((value) => { const answer = arrayLeftRotation.rotLeft( - value.numbers, - value.d_rotations + numbers, + Number(test.d_rotations) ); console.debug( - `rotLeft(${value.numbers.toString()}) solution found: ${answer.toString()}` + `rotLeft(${numbers.toString()}) solution found: ${test.expected.toString()}` ); - expect(answer).toStrictEqual(value.expected); + expect(answer).toStrictEqual(test.expected); }); }); }); diff --git a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json index aa06f8a..086230c 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json +++ b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.testcases.json @@ -1,7 +1,10 @@ [ - {"numbers": [1, 2, 3, 4, 5], "expected": [2, 3, 4, 5, 1]}, - {"numbers": [2, 3, 4, 5, 1], "expected": [3, 4, 5, 1, 2]}, - {"numbers": [3, 4, 5, 1, 2], "expected": [4, 5, 1, 2, 3]}, - {"numbers": [4, 5, 1, 2, 3], "expected": [5, 1, 2, 3, 4]}, - {"numbers": [5, 1, 2, 3, 4], "expected": [1, 2, 3, 4, 5]} + {"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 1", "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]} ] From f820c1876b94c669606fccda82a83035b275f15e Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Fri, 21 Feb 2025 00:02:13 -0300 Subject: [PATCH 05/10] Potential fix for code scanning alert no. 146: Disallow type assertions that do not change the type of an expression Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com> --- .../arrays/ctci_array_left_rotation.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts index 6c629a2..ccd1bcc 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts +++ b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts @@ -10,7 +10,7 @@ describe('ctci_array_left_rotation', () => { expect.assertions(8); ROT_LEFT_TEST_CASES.forEach((test) => { - const numbers = test.input as number[]; + const numbers = test.input; const answer = arrayLeftRotation.rotLeft( numbers, From af262b5a2a1f35367c85bb73045ecaef5dec909e Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Fri, 21 Feb 2025 00:09:39 -0300 Subject: [PATCH 06/10] [REFACTOR] [Hacker Rank] Interview Preparation Kit: Arrays: Left Rotation. New JSON data typed. --- .../arrays/ctci_array_left_rotation.test.ts | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts index ccd1bcc..36475a9 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts +++ b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts @@ -5,20 +5,27 @@ import arrayLeftRotation from './ctci_array_left_rotation'; import ROT_LEFT_TEST_CASES from './ctci_array_left_rotation.testcases.json'; +interface RotLeftTestCase { + title: string; + input: number[]; + d_rotations: number; + expected: number[]; +} + +const TEST_CASES: RotLeftTestCase[] = ROT_LEFT_TEST_CASES as RotLeftTestCase[]; + describe('ctci_array_left_rotation', () => { it('rotLeft Test cases', () => { expect.assertions(8); - ROT_LEFT_TEST_CASES.forEach((test) => { - const numbers = test.input; - + TEST_CASES.forEach((test: RotLeftTestCase) => { const answer = arrayLeftRotation.rotLeft( - numbers, + test.input, Number(test.d_rotations) ); console.debug( - `rotLeft(${numbers.toString()}) solution found: ${test.expected.toString()}` + `rotLeft(${test.input.toString()}) solution found: ${test.expected.toString()}` ); expect(answer).toStrictEqual(test.expected); From cbfe1d22447bf3c6f380d142987e516c505ff6ea Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Fri, 21 Feb 2025 00:22:23 -0300 Subject: [PATCH 07/10] [REFACTOR] [Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Count Triplets. Adjusted the interface to match what hackerrank expects. --- .../count_triplets_1_bruteforce.test.ts | 4 ++-- .../count_triplets_1_bruteforce.ts | 2 +- .../count_triplets_1_optimized.test.ts | 6 +++--- .../dictionaries_and_hashmaps/count_triplets_1_optmized.ts | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.test.ts b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.test.ts index 2705b84..dc910fb 100644 --- a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.test.ts +++ b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from '@jest/globals'; import { logger as console } from '../../../logger'; -import { countTriplets } from './count_triplets_1_bruteforce'; +import CountTriplets from './count_triplets_1_bruteforce'; import SMALL_TEST_CASES from './count_triplets_1.small.testcases.json'; @@ -10,7 +10,7 @@ describe('count_triplets_1', () => { expect.assertions(4); SMALL_TEST_CASES.forEach((test) => { - const answer = countTriplets(test.input, test.r); + const answer = CountTriplets.countTriplets(test.input, test.r); console.debug( `countTriplets(${test.input.toString()}, ${test.r}) solution found: ${answer}` diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.ts b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.ts index ffc33a2..4078098 100644 --- a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.ts +++ b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_bruteforce.ts @@ -4,7 +4,7 @@ */ import { logger as console } from '../../../logger'; -export function countTriplets(arr: number[], ratio: number): number { +function countTriplets(arr: number[], ratio: number): number { const size = arr.length; let counter = 0; diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optimized.test.ts b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optimized.test.ts index ee82d24..6d3676e 100644 --- a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optimized.test.ts +++ b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optimized.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from '@jest/globals'; import { logger as console } from '../../../logger'; -import { countTriplets } from './count_triplets_1_optmized'; +import CountTriplets from './count_triplets_1_optmized'; import SMALL_TEST_CASES from './count_triplets_1.small.testcases.json'; import BIG_TEST_CASES from './count_triplets_1.big.testcases.json'; @@ -11,7 +11,7 @@ describe('count_triplets_1 (optimized)', () => { expect.assertions(4); SMALL_TEST_CASES.forEach((test) => { - const answer = countTriplets(test.input, test.r); + const answer = CountTriplets.countTriplets(test.input, test.r); console.debug( `countTriplets(${test.input.toString()}, ${test.r}) solution found: ${answer}` @@ -25,7 +25,7 @@ describe('count_triplets_1 (optimized)', () => { expect.assertions(1); BIG_TEST_CASES.forEach((test) => { - const answer = countTriplets(test.input, test.r); + const answer = CountTriplets.countTriplets(test.input, test.r); console.debug( `countTriplets(${test.input.toString()}, ${test.r}) solution found: ${answer}` diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optmized.ts b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optmized.ts index 277a80a..a20ea6f 100644 --- a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optmized.ts +++ b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1_optmized.ts @@ -3,7 +3,7 @@ * @see Solution Notes: [[docs/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/count_triplets_1-solution-notes.md]] */ -export function countTriplets(arr: number[], ratio: number): number { +function countTriplets(arr: number[], ratio: number): number { let triplets = 0; const aCounter: Record = arr.reduce( From 92112d67731457c189cbe6b68a1b8592d31e2b4c Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Mon, 3 Mar 2025 16:59:11 -0300 Subject: [PATCH 08/10] [REFACTOR] [Hacker Rank] Interview Preparation Kit: Arrays: Minimum Swaps 2. New Interface. --- .../interview_preparation_kit/arrays/minimum_swaps_2.test.ts | 4 ++-- .../interview_preparation_kit/arrays/minimum_swaps_2.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.test.ts b/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.test.ts index 1b91aaf..a6088b3 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.test.ts +++ b/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from '@jest/globals'; import { logger as console } from '../../../logger'; -import { minimumSwaps } from './minimum_swaps_2'; +import ms2 from './minimum_swaps_2'; import TEST_CASES from './minimum_swaps_2.testcases.json'; @@ -10,7 +10,7 @@ describe('minimum swaps 2', () => { expect.assertions(3); TEST_CASES.forEach((test) => { - const answer = minimumSwaps(test.input); + const answer = ms2.minimumSwaps(test.input); console.debug( `minimumSwaps(${test.input.toString()}) solution found: ${answer}` diff --git a/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.ts b/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.ts index 81f3113c..11f6592 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.ts +++ b/src/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.ts @@ -2,7 +2,7 @@ * @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/minimum_swaps_2.md]] */ -export function minimumSwaps(arr: number[]): number { +function minimumSwaps(arr: number[]): number { const indexedGroup = arr.map((x) => x - 1); let swaps = 0; let index = 0; From 12bc7147412c6a7480a9b8ce4cf9f49587d818fc Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Mon, 3 Mar 2025 17:19:23 -0300 Subject: [PATCH 09/10] [REFACTOR] [Hacker Rank] Interview Preparation Kit: Arrays: New Year Chaos. Adjusted the interface to match what hackerrank expects. --- .../arrays/new_year_chaos.test.ts | 5 +- .../arrays/new_year_chaos.testcases.json | 52 +++++++++---------- .../arrays/new_year_chaos.ts | 23 +++++--- 3 files changed, 44 insertions(+), 36 deletions(-) diff --git a/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.test.ts b/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.test.ts index 3c708d9..fd043c2 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.test.ts +++ b/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from '@jest/globals'; import { logger as console } from '../../../logger'; -import { minimumBribesTransform } from './new_year_chaos'; +import nyc from './new_year_chaos'; import TEST_CASES from './new_year_chaos.testcases.json'; @@ -10,7 +10,8 @@ describe('new_year_chaos', () => { expect.assertions(5); TEST_CASES.forEach((value) => { - const answer = minimumBribesTransform(value.input); + const answer = nyc.minimumBribesText(value.input); + nyc.minimumBribes(value.input); console.debug( `minimumBribesTransform(${value.input.toString()}) solution found: ${answer}` diff --git a/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json b/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json index 5527c31..1679946 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json +++ b/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.testcases.json @@ -1,27 +1,27 @@ [ - { - "title": "Test Case 0-0", - "input": [2, 1, 5, 3, 4], - "expected": 3 - }, - { - "title": "Test Case 0-1", - "input": [2, 5, 1, 3, 4], - "expected": "Too chaotic" - }, - { - "title": "Test Case 1-1", - "input": [5, 1, 2, 3, 7, 8, 6, 4], - "expected": "Too chaotic" - }, - { - "title": "Test Case 1-2", - "input": [1, 2, 5, 3, 7, 8, 6, 4], - "expected": 7 - }, - { - "title": "Test Case 2", - "input": [1, 2, 5, 3, 4, 7, 8, 6], - "expected": 4 - } - ] + { + "title": "Test Case 0-0", + "input": [2, 1, 5, 3, 4], + "expected": "3" + }, + { + "title": "Test Case 0-1", + "input": [2, 5, 1, 3, 4], + "expected": "Too chaotic" + }, + { + "title": "Test Case 1-1", + "input": [5, 1, 2, 3, 7, 8, 6, 4], + "expected": "Too chaotic" + }, + { + "title": "Test Case 1-2", + "input": [1, 2, 5, 3, 7, 8, 6, 4], + "expected": "7" + }, + { + "title": "Test Case 2", + "input": [1, 2, 5, 3, 4, 7, 8, 6], + "expected": "4" + } +] diff --git a/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.ts b/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.ts index b8b3904..2d6c3c4 100644 --- a/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.ts +++ b/src/hackerrank/interview_preparation_kit/arrays/new_year_chaos.ts @@ -2,10 +2,10 @@ * @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/new_year_chaos.md]] */ -export const TOO_CHAOTIC_ERROR = 'Too chaotic'; -export const NEW_YEAR_CHAOS_TOLERANCE = 2; +const TOO_CHAOTIC_ERROR = 'Too chaotic'; +const NEW_YEAR_CHAOS_TOLERANCE = 2; -export function minimumBribes(q: number[]): number { +function minimumBribesCalculate(q: number[]): number { let bribes = 0; let i = 0; @@ -15,7 +15,10 @@ export function minimumBribes(q: number[]): number { throw new Error(TOO_CHAOTIC_ERROR); } - const fragment = q.slice(Math.max(value - NEW_YEAR_CHAOS_TOLERANCE, 0), i); + const fragment = q.slice( + Math.min(Math.max(value - NEW_YEAR_CHAOS_TOLERANCE, 0), i), + i + ); fragment.forEach((k) => { if (k > value) { @@ -28,11 +31,11 @@ export function minimumBribes(q: number[]): number { return bribes; } -export function minimumBribesTransform(queue: number[]): number | string { - let result: number | string = ''; +function minimumBribesText(queue: number[]): string { + let result = ''; try { - result = minimumBribes(queue); + result = `${minimumBribesCalculate(queue)}`; } catch (err: unknown) { if (err instanceof Error) { result = err.message; @@ -42,4 +45,8 @@ export function minimumBribesTransform(queue: number[]): number | string { return result; } -export default { minimumBribes, minimumBribesTransform, TOO_CHAOTIC_ERROR }; +function minimumBribes(q: number[]): void { + console.log(minimumBribesText(q)); +} + +export default { minimumBribes, minimumBribesText, TOO_CHAOTIC_ERROR }; From 67dbda80e7e23d64c26cc6aab6d8aef1655fb302 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Wed, 5 Mar 2025 15:48:18 -0300 Subject: [PATCH 10/10] [REFACTOR] [Hacker Rank] Interview Preparation Kit: Dictionaries and Hashmaps: Two Strings. * Adjusted the interface to match what hackerrank expects. --- .../dictionaries_and_hashmaps/two_strings.test.ts | 4 ++-- .../dictionaries_and_hashmaps/two_strings.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.test.ts b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.test.ts index 2a7b144..0f113c7 100644 --- a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.test.ts +++ b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from '@jest/globals'; import { logger as console } from '../../../logger'; -import { twoStrings } from './two_strings'; +import twoStrings from './two_strings'; import TEST_CASES from './two_strings.testcases.json'; @@ -11,7 +11,7 @@ describe('two_strings', () => { TEST_CASES.forEach((testCase) => { testCase?.test.forEach((test) => { - const answer = twoStrings(test.s1, test.s2); + const answer = twoStrings.twoStrings(test.s1, test.s2); console.debug( `twoStrings(${test.s1}, ${test.s2}) solution found: ${answer}` diff --git a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.ts b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.ts index 878e7ca..e712369 100644 --- a/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.ts +++ b/src/hackerrank/interview_preparation_kit/dictionaries_and_hashmaps/two_strings.ts @@ -5,7 +5,7 @@ const __YES__ = 'YES'; const __NO__ = 'NO'; -export function twoStringsCompute(s1: string, s2: string): boolean { +function twoStringsCompute(s1: string, s2: string): boolean { for (const char of s1) { if (s2.includes(char)) { return true; @@ -14,7 +14,7 @@ export function twoStringsCompute(s1: string, s2: string): boolean { return false; } -export function twoStrings(s1: string, s2: string): string { +function twoStrings(s1: string, s2: string): string { return twoStringsCompute(s1, s2) ? __YES__ : __NO__; }