Skip to content

Commit dcec7c8

Browse files
author
Gonzalo Diaz
committed
[BUGFIX] [Hacker Rank] Interview Preparation Kit: Search: Swap Nodes [Algo]. Disallow reassigning function parameters.
1 parent eac9f5d commit dcec7c8

File tree

2 files changed

+3
-40
lines changed

2 files changed

+3
-40
lines changed

src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,6 @@ export function flatTree(root) {
115115
return output;
116116
}
117117

118-
export function swapBranch(root) {
119-
if (root) {
120-
// eslint-disable-next-line no-param-reassign
121-
[root.left, root.right] = [root.right, root.left];
122-
}
123-
124-
return root;
125-
}
126-
127118
export function swapNodes(indexes, queries) {
128119
const tree = buildTree(indexes);
129120
const output = [];
@@ -152,7 +143,8 @@ export function swapNodes(indexes, queries) {
152143

153144
if (tLevel % query === 0) {
154145
for (const node of nodeList) {
155-
swapBranch(node);
146+
// swap branches
147+
[node.left, node.right] = [node.right, node.left];
156148
}
157149
}
158150
}

src/hackerrank/interview_preparation_kit/search/swap_nodes_algo.test.js

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
import { describe, expect, it } from '@jest/globals';
22

3-
import { Node } from '../../lib/Node.js';
4-
import {
5-
buildTree,
6-
flatTree,
7-
swapBranch,
8-
swapNodes,
9-
__INITIAL_LEVEL__
10-
} from './swap_nodes_algo.js';
3+
import { buildTree, flatTree, swapNodes } from './swap_nodes_algo.js';
114
import TESTCASES from './swap_nodes_algo.testcases.json';
125

136
describe('swap_nodes_algo', () => {
@@ -33,28 +26,6 @@ describe('swap_nodes_algo', () => {
3326
expect(tresult).toStrictEqual(expected);
3427
});
3528

36-
it('testswapBranch empty', () => {
37-
expect.assertions(1);
38-
39-
const tInput = null;
40-
const tresult = swapBranch(tInput);
41-
const expected = null;
42-
43-
expect(tresult).toStrictEqual(expected);
44-
});
45-
46-
it('testswapBranch', () => {
47-
expect.assertions(1);
48-
49-
const tInput = new Node(__INITIAL_LEVEL__);
50-
tInput.left = new Node(2);
51-
tInput.right = new Node(3);
52-
const tresult = flatTree(swapBranch(tInput));
53-
const expected = [3, 1, 2];
54-
55-
expect(tresult).toStrictEqual(expected);
56-
});
57-
5829
it('build tree and flattened tree test cases', () => {
5930
expect.assertions(4);
6031

0 commit comments

Comments
 (0)