Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ describe('partition', () => {
const result = partition(node1, 5);

// Expected partitioned list: 3 -> 2 -> 1 -> 5 -> 8 -> 5 -> 10
expect(result!.value).toEqual(3);
expect(result!.next!.value).toEqual(2);
expect(result!.next!.next!.value).toEqual(1);
expect(result!.next!.next!.next!.value).toEqual(5);
expect(result!.next!.next!.next!.next!.value).toEqual(8);
expect(result!.next!.next!.next!.next!.next!.value).toEqual(5);
expect(result!.next!.next!.next!.next!.next!.next!.value).toEqual(10);
expect(result!.value).toBeLessThan(5);
expect(result!.next!.value).toBeLessThan(5);
expect(result!.next!.next!.value).toBeLessThan(5);
expect(result!.next!.next!.next!.value).toBeGreaterThanOrEqual(5);
expect(result!.next!.next!.next!.next!.value).toBeGreaterThanOrEqual(5);
expect(result!.next!.next!.next!.next!.next!.value).toBeGreaterThanOrEqual(5);
expect(result!.next!.next!.next!.next!.next!.next!.value).toBeGreaterThanOrEqual(5);
});

test('handles single node list correctly', () => {
Expand Down Expand Up @@ -55,11 +55,11 @@ describe('partition', () => {
const result = partition(node1, 6);

// Expected partitioned list: 3 -> 2 -> 1 -> 4 -> 5
expect(result!.value).toEqual(3);
expect(result!.next!.value).toEqual(2);
expect(result!.next!.next!.value).toEqual(1);
expect(result!.next!.next!.next!.value).toEqual(4);
expect(result!.next!.next!.next!.next!.value).toEqual(5);
expect(result!.value).toBeLessThan(6);
expect(result!.next!.value).toBeLessThan(6);
expect(result!.next!.next!.value).toBeLessThan(6);
expect(result!.next!.next!.next!.value).toBeLessThan(6);
expect(result!.next!.next!.next!.next!.value).toBeLessThan(6);
});

test('handles all nodes greater than or equal to x', () => {
Expand All @@ -77,10 +77,10 @@ describe('partition', () => {
const result = partition(node1, 0);

// Expected partitioned list: 3 -> 2 -> 1 -> 4 -> 5
expect(result!.value).toEqual(3);
expect(result!.next!.value).toEqual(2);
expect(result!.next!.next!.value).toEqual(1);
expect(result!.next!.next!.next!.value).toEqual(4);
expect(result!.next!.next!.next!.next!.value).toEqual(5);
expect(result!.value).toBeGreaterThanOrEqual(0);
expect(result!.next!.value).toBeGreaterThanOrEqual(0);
expect(result!.next!.next!.value).toBeGreaterThanOrEqual(0);
expect(result!.next!.next!.next!.value).toBeGreaterThanOrEqual(0);
expect(result!.next!.next!.next!.next!.value).toBeGreaterThanOrEqual(0);
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
import sumListsForwardOrder, { Node } from "../../16_sumListsForwardOrder";

describe("16 - sumListsForwardOrder", () => {
test("Sums one element each without carryover", () => {
const list1: Node<number> = {value: 1}
const list2: Node<number> = {value: 2}
const expected: Node<number> = {value: 3}
const result = sumListsForwardOrder(list1, list2)
expect(result).toEqual(expected)
})

test("Sums two elements each without carryover", () => {
const list1: Node<number> = {value: 1, next: {value: 3}}
const list2: Node<number> = {value: 2, next: {value: 3}}
const expected: Node<number> = {value: 3, next: {value: 6}}
const result = sumListsForwardOrder(list1, list2)
expect(result).toEqual(expected)
})

test("sums two non-empty lists without carryover", () => {
// 123 + 456 = 579
const list1: Node<number> = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ describe('loopDetection', () => {
test('returns null if the list has only one node', () => {
const node: Node<number> = { value: 1 };
const result = loopDetection(node);
expect(result).toBeNull();
expect(result).toBeUndefined();
});

test('returns null if the list does not have a loop', () => {
// List: 1 -> 2 -> 3 -> 4 -> 5
const list: Node<number> = { value: 1, next: { value: 2, next: { value: 3, next: { value: 4, next: { value: 5 } } } } };
const result = loopDetection(list);
expect(result).toBeNull();
expect(result).toBeUndefined();
});

test('returns the node at the beginning of the loop', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ describe("04 - palindromePermutation", () => {
expect(palindromePermutation("RaceCar")).toEqual(true);
});

test("String with repeated letters", () => {
expect(palindromePermutation("rrracecrrar")).toEqual(true);
});

test("String with non-alphanumeric characters", () => {
expect(palindromePermutation("12321")).toEqual(true);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import stringRotation from "../../09_stringRotation";
import {reassignIsSubstring} from "../../__utils__/strings";

describe("09 - stringRotation", () => {
beforeEach(() => {
reassignIsSubstring()
})

test("rotates a string", () => {
const str1 = "Hello";
const str2 = "oHell";
Expand Down
12 changes: 10 additions & 2 deletions technical-fundamentals/coding/problems/__utils__/strings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const createIsSubstring = () => {
export const createIsSubstring = () => {
let called = false

return (s1: string, s2: string) => {
Expand All @@ -17,4 +17,12 @@ const createIsSubstring = () => {
* @param {string} s2 - Substring.
* @return {boolean} If s1 contains s2.
*/
export const isSubstring = createIsSubstring()
export let isSubstring = createIsSubstring()

/**
* For testing purposes we should be able to reset the called flag
* for every test.
*/
export const reassignIsSubstring = () => {
isSubstring=createIsSubstring()
}