[web] qns: update algo question generation script to separate locale agnostic data (#1431)
This commit is contained in:
parent
568cddfd77
commit
e25c1e07e3
File diff suppressed because one or more lines are too long
28
apps/web/src/__generated__/questions/algo/array-balanced-brackets/metadata.json
generated
Normal file
28
apps/web/src/__generated__/questions/algo/array-balanced-brackets/metadata.json
generated
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1722729600,
|
||||
"difficulty": "easy",
|
||||
"duration": 25,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/array-balanced-brackets",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "array-balanced-brackets",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"array",
|
||||
"stack"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/array-balanced-brackets/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/array-balanced-brackets/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
27
apps/web/src/__generated__/questions/algo/array-find-duplicate/metadata.json
generated
Normal file
27
apps/web/src/__generated__/questions/algo/array-find-duplicate/metadata.json
generated
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1718409600,
|
||||
"difficulty": "easy",
|
||||
"duration": 15,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/array-find-duplicate",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "array-find-duplicate",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"array"
|
||||
]
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
27
apps/web/src/__generated__/questions/algo/array-find-missing-number-in-sequence/metadata.json
generated
Normal file
27
apps/web/src/__generated__/questions/algo/array-find-missing-number-in-sequence/metadata.json
generated
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1718928000,
|
||||
"difficulty": "easy",
|
||||
"duration": 15,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/array-find-missing-number-in-sequence",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "array-find-missing-number-in-sequence",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"array"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/array-find-missing-number-in-sequence/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/array-find-missing-number-in-sequence/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
27
apps/web/src/__generated__/questions/algo/array-maximum-product-contiguous/metadata.json
generated
Normal file
27
apps/web/src/__generated__/questions/algo/array-maximum-product-contiguous/metadata.json
generated
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1718236800,
|
||||
"difficulty": "medium",
|
||||
"duration": 30,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/array-maximum-product-contiguous",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "array-maximum-product-contiguous",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"array"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/array-maximum-product-contiguous/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/array-maximum-product-contiguous/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
27
apps/web/src/__generated__/questions/algo/array-maximum-sum-contiguous/metadata.json
generated
Normal file
27
apps/web/src/__generated__/questions/algo/array-maximum-sum-contiguous/metadata.json
generated
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1718841600,
|
||||
"difficulty": "medium",
|
||||
"duration": 25,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/array-maximum-sum-contiguous",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "array-maximum-sum-contiguous",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"array"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/array-maximum-sum-contiguous/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/array-maximum-sum-contiguous/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
28
apps/web/src/__generated__/questions/algo/array-most-common-elements/metadata.json
generated
Normal file
28
apps/web/src/__generated__/questions/algo/array-most-common-elements/metadata.json
generated
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1724889600,
|
||||
"difficulty": "medium",
|
||||
"duration": 35,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/array-most-common-elements",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "array-most-common-elements",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"array",
|
||||
"heap"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/array-most-common-elements/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/array-most-common-elements/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
27
apps/web/src/__generated__/questions/algo/array-product-excluding-current/metadata.json
generated
Normal file
27
apps/web/src/__generated__/questions/algo/array-product-excluding-current/metadata.json
generated
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1718496000,
|
||||
"difficulty": "medium",
|
||||
"duration": 15,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/array-product-excluding-current",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "array-product-excluding-current",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"array"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/array-product-excluding-current/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/array-product-excluding-current/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
29
apps/web/src/__generated__/questions/algo/array-reachable-end/metadata.json
generated
Normal file
29
apps/web/src/__generated__/questions/algo/array-reachable-end/metadata.json
generated
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1720828800,
|
||||
"difficulty": "medium",
|
||||
"duration": 45,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/array-reachable-end",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "array-reachable-end",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"array",
|
||||
"dynamic-programming",
|
||||
"greedy"
|
||||
]
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1718928000,
|
||||
"difficulty": "medium",
|
||||
"duration": 30,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/array-rotated-find",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "array-rotated-find",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"array",
|
||||
"binary-search"
|
||||
]
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
28
apps/web/src/__generated__/questions/algo/array-smallest-in-rotated/metadata.json
generated
Normal file
28
apps/web/src/__generated__/questions/algo/array-smallest-in-rotated/metadata.json
generated
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1718928000,
|
||||
"difficulty": "medium",
|
||||
"duration": 25,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/array-smallest-in-rotated",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "array-smallest-in-rotated",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"array",
|
||||
"binary-search"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/array-smallest-in-rotated/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/array-smallest-in-rotated/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
29
apps/web/src/__generated__/questions/algo/binary-search-tree-kth-smallest/metadata.json
generated
Normal file
29
apps/web/src/__generated__/questions/algo/binary-search-tree-kth-smallest/metadata.json
generated
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1724284800,
|
||||
"difficulty": "medium",
|
||||
"duration": 35,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/binary-search-tree-kth-smallest",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "binary-search-tree-kth-smallest",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary-tree",
|
||||
"binary-search-tree",
|
||||
"tree"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/binary-search-tree-kth-smallest/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/binary-search-tree-kth-smallest/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
29
apps/web/src/__generated__/questions/algo/binary-search-tree-lowest-common-ancestor/metadata.json
generated
Normal file
29
apps/web/src/__generated__/questions/algo/binary-search-tree-lowest-common-ancestor/metadata.json
generated
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1724284800,
|
||||
"difficulty": "medium",
|
||||
"duration": 35,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/binary-search-tree-lowest-common-ancestor",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "binary-search-tree-lowest-common-ancestor",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary-tree",
|
||||
"binary-search-tree",
|
||||
"tree"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/binary-search-tree-lowest-common-ancestor/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/binary-search-tree-lowest-common-ancestor/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
29
apps/web/src/__generated__/questions/algo/binary-search-tree-validate/metadata.json
generated
Normal file
29
apps/web/src/__generated__/questions/algo/binary-search-tree-validate/metadata.json
generated
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1724284800,
|
||||
"difficulty": "medium",
|
||||
"duration": 35,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/binary-search-tree-validate",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "binary-search-tree-validate",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary-tree",
|
||||
"binary-search-tree",
|
||||
"tree"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/binary-search-tree-validate/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/binary-search-tree-validate/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,32 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": null,
|
||||
"companies": [],
|
||||
"created": 1745539200,
|
||||
"difficulty": "medium",
|
||||
"duration": 15,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/binary-search-tree",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [
|
||||
"heap",
|
||||
"binary-search"
|
||||
],
|
||||
"slug": "binary-search-tree",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary-tree",
|
||||
"binary-search-tree",
|
||||
"tree"
|
||||
]
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"files": {
|
||||
"/package.json": "{\n \"name\": \"@gfe-questions/binary-search-tree\",\n \"version\": \"0.0.1\",\n \"main\": \"/src/binary-search-tree.ts\",\n \"devDependencies\": {\n \"@types/jest\": \"29.5.0\",\n \"typescript\": \"5.0.2\"\n }\n}\n",
|
||||
"/tsconfig.json": "{\n \"include\": [\"./**/*\"],\n \"compilerOptions\": {\n \"strict\": true,\n \"esModuleInterop\": true,\n \"lib\": [\"dom\", \"dom.iterable\", \"esnext\"],\n \"jsx\": \"react-jsx\",\n \"target\": \"es2020\"\n }\n}\n",
|
||||
"/src/binary-search-tree.run.test.ts": "import BST from './binary-search-tree';\n\ndescribe('BST', () => {\n test('constructor', () => {\n const bst = new BST();\n expect(bst instanceof BST).toBeTruthy();\n });\n\n test('insert and search', () => {\n const bst = new BST();\n bst.insert(100);\n expect(bst.search(100)).toBeTruthy();\n bst.insert(200);\n expect(bst.search(200)).toBeTruthy();\n bst.insert(50);\n expect(bst.search(50)).toBeTruthy();\n expect(bst.search(250)).toBeFalsy();\n });\n\n test('delete', () => {\n const bst = new BST();\n bst.insert(300);\n bst.insert(100);\n bst.insert(200);\n bst.insert(50);\n bst.delete(100);\n expect(bst.search(100)).toBeFalsy();\n expect(bst.search(300)).toBeTruthy();\n expect(bst.search(200)).toBeTruthy();\n bst.delete(300);\n expect(bst.search(300)).toBeFalsy();\n expect(bst.search(50)).toBeTruthy();\n });\n});\n",
|
||||
"/src/binary-search-tree.submit.test.ts": "import BST from './binary-search-tree';\n\ndescribe('BST', () => {\n test('constructor', () => {\n const bst = new BST();\n expect(bst instanceof BST).toBeTruthy();\n expect(bst.root).toBeNull();\n });\n\n test('insert and search', () => {\n const bst = new BST();\n bst.insert(100);\n expect(bst.search(100)).toBeTruthy();\n bst.insert(200);\n expect(bst.search(200)).toBeTruthy();\n bst.insert(50);\n expect(bst.search(50)).toBeTruthy();\n });\n\n test('delete leaf node', () => {\n const bst = new BST();\n bst.insert(50);\n bst.insert(30);\n bst.insert(70);\n bst.delete(30);\n expect(bst.search(30)).toBeFalsy();\n expect(bst.root?.left).toBeNull();\n });\n\n test('delete node with one child', () => {\n const bst = new BST();\n bst.insert(50);\n bst.insert(30);\n bst.insert(20);\n bst.delete(30);\n expect(bst.search(30)).toBeFalsy();\n expect(bst.root?.left?.value).toBe(20);\n });\n\n test('delete node with two children', () => {\n const bst = new BST();\n bst.insert(50);\n bst.insert(30);\n bst.insert(70);\n bst.insert(20);\n bst.insert(40);\n bst.delete(30);\n expect(bst.search(30)).toBeFalsy();\n expect(bst.root?.left?.value).not.toBe(30);\n });\n\n test('delete root node', () => {\n const bst = new BST();\n bst.insert(50);\n bst.insert(30);\n bst.insert(70);\n bst.delete(50);\n expect(bst.search(50)).toBeFalsy();\n expect(bst.root?.value).not.toBe(50);\n });\n\n test('insert many and test structure', () => {\n const bst = new BST();\n const values = [50, 30, 70, 20, 40, 60, 80];\n values.forEach((value) => bst.insert(value));\n expect(bst.root?.value).toBe(50);\n expect(bst.root?.left?.value).toBe(30);\n expect(bst.root?.right?.value).toBe(70);\n expect(bst.root?.left?.left?.value).toBe(20);\n expect(bst.root?.left?.right?.value).toBe(40);\n expect(bst.root?.right?.left?.value).toBe(60);\n expect(bst.root?.right?.right?.value).toBe(80);\n });\n\n test('search non-existent value', () => {\n const bst = new BST();\n bst.insert(50);\n expect(bst.search(100)).toBeFalsy();\n });\n\n test('complex insert and delete sequence', () => {\n const bst = new BST();\n bst.insert(50);\n bst.insert(30);\n bst.insert(70);\n bst.insert(20);\n bst.insert(40);\n bst.insert(60);\n bst.insert(80);\n bst.delete(70);\n expect(bst.search(70)).toBeFalsy();\n bst.delete(50);\n expect(bst.search(50)).toBeFalsy();\n bst.insert(35);\n bst.insert(45);\n expect(bst.search(35)).toBeTruthy();\n expect(bst.search(45)).toBeTruthy();\n });\n\n test('delete on empty BST', () => {\n const bst = new BST();\n bst.delete(100);\n expect(bst.root).toBeNull();\n });\n\n test('maintaining state after multiple operations', () => {\n const bst = new BST();\n bst.insert(50);\n bst.insert(30);\n bst.insert(60);\n bst.delete(50);\n bst.insert(55);\n bst.insert(65);\n expect(bst.search(55)).toBeTruthy();\n expect(bst.search(65)).toBeTruthy();\n bst.delete(65);\n expect(bst.search(65)).toBeFalsy();\n });\n\n test('integration test of operations', () => {\n const bst = new BST();\n bst.insert(50);\n bst.insert(30);\n bst.insert(70);\n bst.insert(20);\n bst.insert(60);\n bst.insert(80);\n bst.insert(40);\n bst.delete(50);\n expect(bst.search(50)).toBeFalsy();\n bst.insert(55);\n expect(bst.search(55)).toBeTruthy();\n bst.delete(55);\n expect(bst.search(55)).toBeFalsy();\n });\n});\n",
|
||||
"/src/binary-search-tree.ts": "class Node<T> {\n value: number | null;\n left: Node<T> | null;\n right: Node<T> | null;\n\n constructor(value: number | null = null) {\n this.value = value;\n this.left = null;\n this.right = null;\n }\n}\n\nexport default class BinarySearchTree<T> {\n root: Node<T> | null;\n\n constructor() {\n this.root = null;\n }\n\n /**\n * Inserts a new value into the BST while maintaining BST properties.\n * @param value The value to be inserted into the BST.\n */\n insert(value: number): void {\n const newNode = new Node(value);\n if (this.root === null) {\n this.root = newNode;\n return;\n }\n\n let currentNode: Node<T> | null = this.root;\n let parent: Node<T> | null = null;\n while (currentNode) {\n parent = currentNode;\n if (value < currentNode.value!) {\n currentNode = currentNode.left;\n } else {\n currentNode = currentNode.right;\n }\n }\n\n if (parent && value < parent.value!) {\n parent.left = newNode;\n } else if (parent) {\n parent.right = newNode;\n }\n }\n\n /**\n * Searches for a value in the BST. Returns true if the value exists, false otherwise.\n * @param value The value to search for.\n * @return True if the value is found, false otherwise.\n */\n search(value: number): boolean {\n let currentNode = this.root;\n while (currentNode) {\n if (value === currentNode.value) {\n return true;\n }\n currentNode =\n value < currentNode.value! ? currentNode.left : currentNode.right;\n }\n return false;\n }\n\n /**\n * Deletes a value from the BST, if it exists, while maintaining BST properties.\n * @param value The value to be deleted from the BST.\n */\n delete(value: number): void {\n let currentNode = this.root;\n let parent: Node<T> | null = null;\n\n // Find the node and its parent.\n while (currentNode && currentNode.value !== value) {\n parent = currentNode;\n if (value < currentNode.value!) {\n currentNode = currentNode.left;\n } else {\n currentNode = currentNode.right;\n }\n }\n\n if (!currentNode) {\n return; // Node not found.\n }\n\n // Node has two children.\n if (currentNode.left && currentNode.right) {\n let successor = currentNode.right;\n let successorParent = currentNode;\n\n // Find the node with the smallest value in the right subtree and take note of its parent.\n while (successor.left) {\n successorParent = successor;\n successor = successor.left;\n }\n\n currentNode.value = successor.value; // Replace value.\n currentNode = successor; // Move pointer to successor, which will be deleted.\n parent = successorParent;\n }\n\n // Node has one or zero children.\n let child = currentNode.left ? currentNode.left : currentNode.right;\n\n // If the node to be deleted is the root node.\n if (!parent) {\n this.root = child;\n } else {\n if (parent.left === currentNode) {\n parent.left = child;\n } else {\n parent.right = child;\n }\n }\n }\n}\n"
|
||||
},
|
||||
"skeleton": {
|
||||
"js": "export default class BinarySearchTree {\n constructor() {\n throw 'Not implemented!';\n }\n\n /**\n * Inserts a new value into the BST while maintaining BST properties.\n * @param {*} value The value to be inserted into the BST.\n */\n insert(value) {\n throw 'Not implemented!';\n }\n\n /**\n * Searches for a value in the BST. Returns true if the value exists, false otherwise.\n * @param {*} value The value to search for.\n * @return {boolean} True if the value is found, false otherwise.\n */\n search(value) {\n throw 'Not implemented!';\n }\n\n /**\n * Deletes a value from the BST, if it exists, while maintaining BST properties.\n * @param {*} value The value to be deleted from the BST.\n */\n delete(value) {\n throw 'Not implemented!';\n }\n}",
|
||||
"ts": "export default class BinarySearchTree<T> {\n constructor() {\n throw 'Not implemented!';\n }\n\n /**\n * Inserts a new value into the BST while maintaining BST properties.\n * @param value The value to be inserted into the BST.\n */\n insert(value: number): void {\n throw 'Not implemented!';\n }\n\n /**\n * Searches for a value in the BST. Returns true if the value exists, false otherwise.\n * @param value The value to search for.\n * @return True if the value is found, false otherwise.\n */\n search(value: number): boolean {\n throw 'Not implemented!';\n }\n\n /**\n * Deletes a value from the BST, if it exists, while maintaining BST properties.\n * @param value The value to be deleted from the BST.\n */\n delete(value: number): void {\n throw 'Not implemented!';\n }\n}"
|
||||
},
|
||||
"workspace": {
|
||||
"main": "/src/binary-search-tree.ts",
|
||||
"run": "/src/binary-search-tree.run.test.ts",
|
||||
"submit": "/src/binary-search-tree.submit.test.ts"
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,34 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": null,
|
||||
"companies": [],
|
||||
"created": 1671667200,
|
||||
"difficulty": "medium",
|
||||
"duration": 15,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/binary-search",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [
|
||||
"selection-sort",
|
||||
"quick-sort",
|
||||
"merge-sort",
|
||||
"depth-first-search",
|
||||
"breadth-first-search"
|
||||
],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "binary-search",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary-search",
|
||||
"sorting"
|
||||
]
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"files": {
|
||||
"/package.json": "{\n \"name\": \"@gfe-questions/binary-search\",\n \"version\": \"0.0.1\",\n \"main\": \"/src/binary-search.ts\",\n \"devDependencies\": {\n \"@types/jest\": \"29.5.0\",\n \"typescript\": \"5.0.2\"\n }\n}\n",
|
||||
"/tsconfig.json": "{\n \"include\": [\"./**/*\"],\n \"compilerOptions\": {\n \"strict\": true,\n \"esModuleInterop\": true,\n \"lib\": [\"dom\", \"dom.iterable\", \"esnext\"],\n \"jsx\": \"react-jsx\",\n \"target\": \"es2020\"\n }\n}\n",
|
||||
"/src/binary-search.run.test.ts": "import binarySearch from './binary-search';\n\ndescribe('binarySearch', () => {\n test('target value', () => {\n expect(binarySearch([1, 2, 3, 10, 11, 20], 2)).toBe(1);\n });\n\n test('boundary value', () => {\n expect(binarySearch([1, 2, 3, 10, 11, 20], 20)).toBe(5);\n });\n\n test('non-existent value', () => {\n expect(binarySearch([1, 2, 3, 10, 11, 20], 9)).toBe(-1);\n });\n});\n",
|
||||
"/src/binary-search.submit.test.ts": "import binarySearch from './binary-search';\n\ndescribe('binarySearch', () => {\n test('empty', () => {\n expect(binarySearch([], 1)).toBe(-1);\n });\n\n test('one element', () => {\n expect(binarySearch([1], 1)).toBe(0);\n expect(binarySearch([1], 2)).toBe(-1);\n });\n\n test('two elements', () => {\n expect(binarySearch([1, 4], 1)).toBe(0);\n expect(binarySearch([1, 4], 4)).toBe(1);\n expect(binarySearch([1, 4], 5)).toBe(-1);\n });\n\n test('more than two elements', () => {\n expect(binarySearch([1, 2, 3, 10, 11, 20], 1)).toBe(0);\n expect(binarySearch([1, 2, 3, 10, 11, 20], 2)).toBe(1);\n expect(binarySearch([1, 2, 3, 10, 11, 20], 3)).toBe(2);\n expect(binarySearch([1, 2, 3, 10, 11, 20], 10)).toBe(3);\n expect(binarySearch([1, 2, 3, 10, 11, 20], 9)).toBe(-1);\n expect(binarySearch([1, 2, 3, 10, 11, 20], 4)).toBe(-1);\n expect(binarySearch([1, 2, 3, 10, 11, 20], 0)).toBe(-1);\n expect(binarySearch([1, 2, 3, 10, 11, 20], 21)).toBe(-1);\n });\n\n test('boundary values', () => {\n expect(binarySearch([1, 2, 3, 10, 11, 20], 1)).toBe(0);\n expect(binarySearch([1, 2, 3, 10, 11, 20], 20)).toBe(5);\n });\n});\n",
|
||||
"/src/binary-search.ts": "export default function binarySearch(\n arr: Array<number>,\n target: number,\n): number {\n // Initialize the left and right indices of the array\n let left = 0;\n let right = arr.length - 1;\n\n // Keep searching until the left and right indices meet.\n while (left <= right) {\n // Calculate the mid index to retrieve the mid element later.\n const mid = Math.floor((left + right) / 2);\n\n if (target < arr[mid]) {\n // If the target element is less than the middle element,\n // search the left half of the array.\n // Adjust the right index so the next loop iteration\n // searches the left side.\n right = mid - 1;\n } else if (target > arr[mid]) {\n // If the target element is greater than the middle element,\n // search the right half of the array.\n // Adjust the left index so the next loop iteration\n // searches the left side.\n left = mid + 1;\n } else {\n // If the target element is equal to the middle element,\n // return the index of the middle element.\n return mid;\n }\n }\n\n // If the element is not found, return -1.\n return -1;\n}\n"
|
||||
},
|
||||
"skeleton": {
|
||||
"js": "/**\n * @param {Array<number>} arr The input integer array to be searched.\n * @param {number} target The target integer to search within the array.\n * @return {number} The index of target element in the array, or -1 if not found.\n */\nexport default function binarySearch(arr, target) {\n throw 'Not implemented!';\n}",
|
||||
"ts": "/**\n * @param arr The input integer array to be searched.\n * @param target The target integer to search within the array.\n * @return The index of target element in the array, or -1 if not found.\n */\nexport default function binarySearch(\n arr: Array<number>,\n target: number,\n): number {\n throw 'Not implemented!';\n}"
|
||||
},
|
||||
"workspace": {
|
||||
"main": "/src/binary-search.ts",
|
||||
"run": "/src/binary-search.run.test.ts",
|
||||
"submit": "/src/binary-search.submit.test.ts"
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1723161600,
|
||||
"difficulty": "easy",
|
||||
"duration": 25,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/binary-tree-equal",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "binary-tree-equal",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary-tree",
|
||||
"tree"
|
||||
]
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1723766400,
|
||||
"difficulty": "easy",
|
||||
"duration": 20,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/binary-tree-flip",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "binary-tree-flip",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary-tree",
|
||||
"tree"
|
||||
]
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
28
apps/web/src/__generated__/questions/algo/binary-tree-level-order-traversal/metadata.json
generated
Normal file
28
apps/web/src/__generated__/questions/algo/binary-tree-level-order-traversal/metadata.json
generated
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1723161600,
|
||||
"difficulty": "medium",
|
||||
"duration": 25,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/binary-tree-level-order-traversal",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "binary-tree-level-order-traversal",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary-tree",
|
||||
"tree"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/binary-tree-level-order-traversal/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/binary-tree-level-order-traversal/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
29
apps/web/src/__generated__/questions/algo/binary-tree-maximum-depth/metadata.json
generated
Normal file
29
apps/web/src/__generated__/questions/algo/binary-tree-maximum-depth/metadata.json
generated
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1723161600,
|
||||
"difficulty": "easy",
|
||||
"duration": 20,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/binary-tree-maximum-depth",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "binary-tree-maximum-depth",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary-tree",
|
||||
"tree",
|
||||
"dfs"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/binary-tree-maximum-depth/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/binary-tree-maximum-depth/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
29
apps/web/src/__generated__/questions/algo/binary-tree-maximum-total-path/metadata.json
generated
Normal file
29
apps/web/src/__generated__/questions/algo/binary-tree-maximum-total-path/metadata.json
generated
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1724112000,
|
||||
"difficulty": "hard",
|
||||
"duration": 40,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/binary-tree-maximum-total-path",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "binary-tree-maximum-total-path",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary-tree",
|
||||
"tree",
|
||||
"dfs"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/binary-tree-maximum-total-path/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/binary-tree-maximum-total-path/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
28
apps/web/src/__generated__/questions/algo/binary-tree-rebuilding-from-traversals/metadata.json
generated
Normal file
28
apps/web/src/__generated__/questions/algo/binary-tree-rebuilding-from-traversals/metadata.json
generated
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1724889600,
|
||||
"difficulty": "medium",
|
||||
"duration": 45,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/binary-tree-rebuilding-from-traversals",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "binary-tree-rebuilding-from-traversals",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary-tree",
|
||||
"tree"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/binary-tree-rebuilding-from-traversals/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/binary-tree-rebuilding-from-traversals/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
29
apps/web/src/__generated__/questions/algo/binary-tree-serialization-deserialization/metadata.json
generated
Normal file
29
apps/web/src/__generated__/questions/algo/binary-tree-serialization-deserialization/metadata.json
generated
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1724889600,
|
||||
"difficulty": "hard",
|
||||
"duration": 20,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/binary-tree-serialization-deserialization",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "binary-tree-serialization-deserialization",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary-tree",
|
||||
"tree",
|
||||
"dfs"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/binary-tree-serialization-deserialization/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/binary-tree-serialization-deserialization/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
28
apps/web/src/__generated__/questions/algo/binary-tree-subtree/metadata.json
generated
Normal file
28
apps/web/src/__generated__/questions/algo/binary-tree-subtree/metadata.json
generated
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1723852800,
|
||||
"difficulty": "easy",
|
||||
"duration": 35,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/binary-tree-subtree",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "binary-tree-subtree",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary-tree",
|
||||
"tree"
|
||||
]
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,31 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": null,
|
||||
"companies": [],
|
||||
"created": 1716422400,
|
||||
"difficulty": "medium",
|
||||
"duration": 15,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/binary-tree",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [
|
||||
"binary-search-tree",
|
||||
"binary-search"
|
||||
],
|
||||
"slug": "binary-tree",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary-tree",
|
||||
"tree"
|
||||
]
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1718928000,
|
||||
"difficulty": "easy",
|
||||
"duration": 25,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/bit-counting",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "bit-counting",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary"
|
||||
]
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1718928000,
|
||||
"difficulty": "easy",
|
||||
"duration": 25,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/bit-reversal",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "bit-reversal",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary"
|
||||
]
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
29
apps/web/src/__generated__/questions/algo/breadth-first-search/metadata.json
generated
Normal file
29
apps/web/src/__generated__/questions/algo/breadth-first-search/metadata.json
generated
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"access": "free",
|
||||
"author": null,
|
||||
"companies": [],
|
||||
"created": 1679788800,
|
||||
"difficulty": "medium",
|
||||
"duration": 15,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/breadth-first-search",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [
|
||||
"depth-first-search"
|
||||
],
|
||||
"slug": "breadth-first-search",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"bfs"
|
||||
]
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"files": {
|
||||
"/package.json": "{\n \"name\": \"@gfe-questions/breadth-first-search\",\n \"version\": \"0.0.1\",\n \"main\": \"/src/breadth-first-search.ts\",\n \"devDependencies\": {\n \"@types/jest\": \"29.5.0\",\n \"typescript\": \"5.0.2\"\n }\n}\n",
|
||||
"/tsconfig.json": "{\n \"include\": [\"./**/*\"],\n \"compilerOptions\": {\n \"strict\": true,\n \"esModuleInterop\": true,\n \"lib\": [\"dom\", \"dom.iterable\", \"esnext\"],\n \"jsx\": \"react-jsx\",\n \"target\": \"es2020\"\n }\n}\n",
|
||||
"/src/breadth-first-search.run.test.ts": "import breadthFirstSearch from './breadth-first-search';\n\ndescribe('breadthFirstSearch', () => {\n test('single node graph', () => {\n expect(breadthFirstSearch({ A: [] }, 'A')).toEqual(['A']);\n });\n\n test('two node graph', () => {\n expect(breadthFirstSearch({ A: ['B'], B: [] }, 'A')).toEqual(['A', 'B']);\n });\n\n test('multiple node graph', () => {\n const graph = {\n A: ['B', 'C'],\n B: ['D', 'E'],\n C: ['F', 'G'],\n D: [],\n E: [],\n F: [],\n G: [],\n };\n expect(breadthFirstSearch(graph, 'A')).toEqual([\n 'A',\n 'B',\n 'C',\n 'D',\n 'E',\n 'F',\n 'G',\n ]);\n });\n});\n",
|
||||
"/src/breadth-first-search.submit.test.ts": "import breadthFirstSearch from './breadth-first-search';\n\ndescribe('breadthFirstSearch', () => {\n test('empty graph', () => {\n expect(breadthFirstSearch({}, '')).toEqual([]);\n });\n\n test('graphs with one node', () => {\n expect(breadthFirstSearch({ A: [] }, 'A')).toEqual(['A']);\n });\n\n test('graphs with two nodes', () => {\n expect(breadthFirstSearch({ A: ['B'], B: [] }, 'A')).toEqual(['A', 'B']);\n expect(breadthFirstSearch({ A: ['A', 'B'], B: [] }, 'A')).toEqual([\n 'A',\n 'B',\n ]);\n expect(breadthFirstSearch({ A: ['A', 'B'], B: [] }, 'B')).toEqual(['B']);\n expect(breadthFirstSearch({ A: ['A', 'B'], B: ['A'] }, 'B')).toEqual([\n 'B',\n 'A',\n ]);\n });\n\n test('graphs with multiple nodes', () => {\n expect(breadthFirstSearch({ A: ['B'], B: ['C'], C: [] }, 'A')).toEqual([\n 'A',\n 'B',\n 'C',\n ]);\n expect(breadthFirstSearch({ A: ['B', 'C'], B: [], C: [] }, 'A')).toEqual([\n 'A',\n 'B',\n 'C',\n ]);\n expect(\n breadthFirstSearch(\n { A: ['B', 'C'], B: [], C: [], D: ['B'], E: ['C'] },\n 'A',\n ),\n ).toEqual(['A', 'B', 'C']);\n expect(\n breadthFirstSearch(\n { A: ['D', 'E'], B: [], C: [], D: ['B'], E: ['C'] },\n 'A',\n ),\n ).toEqual(['A', 'D', 'E', 'B', 'C']);\n expect(\n breadthFirstSearch(\n {\n A: ['D', 'E'],\n B: ['A', 'B', 'C', 'D', 'E'],\n C: [],\n D: ['B'],\n E: ['C'],\n },\n 'A',\n ),\n ).toEqual(['A', 'D', 'E', 'B', 'C']);\n // Graph taken from https://www.geeksforgeeks.org/breadth-first-traversal-for-a-graph/\n const graph = {\n A: ['B', 'C'],\n B: ['A', 'D', 'E'],\n C: ['A', 'E'],\n D: ['B', 'E', 'F'],\n E: ['B', 'C', 'D', 'F'],\n F: ['D', 'E'],\n };\n expect(breadthFirstSearch(graph, 'A')).toEqual([\n 'A',\n 'B',\n 'C',\n 'D',\n 'E',\n 'F',\n ]);\n expect(breadthFirstSearch(graph, 'B')).toEqual([\n 'B',\n 'A',\n 'D',\n 'E',\n 'C',\n 'F',\n ]);\n expect(breadthFirstSearch(graph, 'C')).toEqual([\n 'C',\n 'A',\n 'E',\n 'B',\n 'D',\n 'F',\n ]);\n expect(breadthFirstSearch(graph, 'D')).toEqual([\n 'D',\n 'B',\n 'E',\n 'F',\n 'A',\n 'C',\n ]);\n });\n\n test('disjoint graphs', () => {\n const disjoinGraph = { A: ['B'], B: [], C: [], D: ['C'] };\n expect(breadthFirstSearch(disjoinGraph, 'A')).toEqual(['A', 'B']);\n expect(breadthFirstSearch(disjoinGraph, 'C')).toEqual(['C']);\n expect(breadthFirstSearch(disjoinGraph, 'D')).toEqual(['D', 'C']);\n });\n\n test('cyclic graphs', () => {\n expect(breadthFirstSearch({ A: ['A'] }, 'A')).toEqual(['A']);\n const cyclicGraphOne = {\n A: ['B', 'C', 'D'],\n B: ['E', 'F'],\n C: ['G', 'H'],\n D: ['I', 'J'],\n E: ['D'],\n F: [],\n G: [],\n H: [],\n I: [],\n J: [],\n };\n const cyclicGraphTwo = {\n A: ['B', 'C'],\n B: ['D', 'E'],\n C: ['F', 'G'],\n D: [],\n E: [],\n F: [],\n G: [],\n };\n expect(breadthFirstSearch(cyclicGraphOne, 'A')).toEqual([\n 'A',\n 'B',\n 'C',\n 'D',\n 'E',\n 'F',\n 'G',\n 'H',\n 'I',\n 'J',\n ]);\n expect(breadthFirstSearch(cyclicGraphOne, 'B')).toEqual([\n 'B',\n 'E',\n 'F',\n 'D',\n 'I',\n 'J',\n ]);\n expect(breadthFirstSearch(cyclicGraphTwo, 'A')).toEqual([\n 'A',\n 'B',\n 'C',\n 'D',\n 'E',\n 'F',\n 'G',\n ]);\n expect(breadthFirstSearch(cyclicGraphTwo, 'E')).toEqual(['E']);\n });\n});\n",
|
||||
"/src/breadth-first-search.ts": "export default function breadthFirstSearch(\n graph: Record<string, Array<string>>,\n source: string,\n): Array<string> {\n // If there are no nodes in the graph, just return an empty array\n if (Object.keys(graph).length === 0) {\n return [];\n }\n\n // Create a queue to store the nodes to be visited.\n // Add the root node since we're doing a level-order BFS.\n const queue = new Queue<string>();\n queue.enqueue(source);\n\n // Initialize a set that tracks visited nodes.\n const visited = new Set<string>();\n\n // While there are nodes to visit.\n while (!queue.isEmpty()) {\n // Dequeue the node at the front of the queue.\n const node = queue.dequeue()!;\n\n // Mark the node as visited.\n visited.add(node);\n\n // Enqueue the neighbors of the current node.\n graph[node].forEach((neighbor) => {\n // Skip nodes that have already been visited.\n if (visited.has(neighbor)) {\n return;\n }\n\n queue.enqueue(neighbor);\n });\n }\n\n // The visited nodes is the traversal order.\n return Array.from(visited);\n}\n\n/* Auxiliary classes */\n\n/**\n * A Queue class with O(1) enqueue and dequeue is provided for you.\n * You can use it directly should you wish to.\n *\n * Example usage:\n * const q = new Queue();\n * q.enqueue('a');\n * q.enqueue('b');\n * q.dequeue(); //'a'\n * q.isEmpty(); // False\n */\nclass Node<T> {\n value: T;\n next: Node<T> | null;\n\n constructor(value: T) {\n this.value = value;\n this.next = null;\n }\n}\n\nclass Queue<T> {\n head: Node<T> | null;\n tail: Node<T> | null;\n length: number;\n\n constructor() {\n this.head = null;\n this.tail = null;\n this.length = 0;\n }\n\n isEmpty(): boolean {\n return this.length === 0;\n }\n\n enqueue(item: T): void {\n const newNode = new Node(item);\n if (this.isEmpty()) {\n this.head = newNode;\n } else if (this.tail) {\n this.tail.next = newNode;\n }\n this.tail = newNode;\n this.length++;\n }\n\n dequeue(): T | null {\n if (this.isEmpty() || !this.head) {\n return null;\n } else {\n const removedNode = this.head;\n this.head = this.head.next;\n removedNode.next = null;\n this.length--;\n if (this.isEmpty()) {\n this.tail = null;\n }\n return removedNode.value;\n }\n }\n}\n"
|
||||
},
|
||||
"skeleton": {
|
||||
"js": "/**\n * @param {Record<string, Array<string>} graph The adjacency list representing the graph.\n * @param {string} source The source node to start traversal from. Has to be a valid node if graph is non-empty.\n * @return {Array<string>} A BFS-traversed order of nodes.\n */\nexport default function breadthFirstSearch(graph, source) {\n throw 'Not implemented!';\n}\n\n/* Auxiliary classes */\n\n/**\n * A Queue class with O(1) enqueue and dequeue is provided for you.\n * You can use it directly should you wish to.\n *\n * Example usage:\n * const q = new Queue();\n * q.enqueue('a');\n * q.enqueue('b');\n * q.dequeue(); //'a'\n * q.isEmpty(); // False\n */\nclass Node {\n constructor(value) {\n this.value = value;\n this.next = null;\n }\n}\n\nclass Queue {\n constructor() {\n this.head = null;\n this.tail = null;\n this.length = 0;\n }\n\n isEmpty() {\n return this.length === 0;\n }\n\n enqueue(item) {\n const newNode = new Node(item);\n if (this.isEmpty()) {\n this.head = newNode;\n } else if (this.tail) {\n this.tail.next = newNode;\n }\n this.tail = newNode;\n this.length++;\n }\n\n dequeue() {\n if (this.isEmpty() || !this.head) {\n return null;\n } else {\n const removedNode = this.head;\n this.head = this.head.next;\n removedNode.next = null;\n this.length--;\n if (this.isEmpty()) {\n this.tail = null;\n }\n return removedNode.value;\n }\n }\n}",
|
||||
"ts": "/**\n * @param graph The adjacency list representing the graph.\n * @param source The source node to start traversal from. Has to be a valid node if graph is non-empty.\n * @return A BFS-traversed order of nodes.\n */\nexport default function breadthFirstSearch(\n graph: Record<string, Array<string>>,\n source: string,\n): Array<string> {\n throw 'Not implemented!';\n}\n\n/* Auxiliary classes */\n\n/**\n * A Queue class with O(1) enqueue and dequeue is provided for you.\n * You can use it directly should you wish to.\n *\n * Example usage:\n * const q = new Queue();\n * q.enqueue('a');\n * q.enqueue('b');\n * q.dequeue(); //'a'\n * q.isEmpty(); // False\n */\nclass Node<T> {\n value: T;\n next: Node<T> | null;\n\n constructor(value: T) {\n this.value = value;\n this.next = null;\n }\n}\n\nclass Queue<T> {\n head: Node<T> | null;\n tail: Node<T> | null;\n length: number;\n\n constructor() {\n this.head = null;\n this.tail = null;\n this.length = 0;\n }\n\n isEmpty(): boolean {\n return this.length === 0;\n }\n\n enqueue(item: T): void {\n const newNode = new Node(item);\n if (this.isEmpty()) {\n this.head = newNode;\n } else if (this.tail) {\n this.tail.next = newNode;\n }\n this.tail = newNode;\n this.length++;\n }\n\n dequeue(): T | null {\n if (this.isEmpty() || !this.head) {\n return null;\n } else {\n const removedNode = this.head;\n this.head = this.head.next;\n removedNode.next = null;\n this.length--;\n if (this.isEmpty()) {\n this.tail = null;\n }\n return removedNode.value;\n }\n }\n}"
|
||||
},
|
||||
"workspace": {
|
||||
"main": "/src/breadth-first-search.ts",
|
||||
"run": "/src/breadth-first-search.run.test.ts",
|
||||
"submit": "/src/breadth-first-search.submit.test.ts"
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,32 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": null,
|
||||
"companies": [],
|
||||
"created": 1745539200,
|
||||
"difficulty": "easy",
|
||||
"duration": 15,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/bubble-sort",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [
|
||||
"quick-sort"
|
||||
],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [
|
||||
"insertion-sort",
|
||||
"selection-sort"
|
||||
],
|
||||
"slug": "bubble-sort",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"sorting"
|
||||
]
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"files": {
|
||||
"/package.json": "{\n \"name\": \"@gfe-questions/bubble-sort\",\n \"version\": \"0.0.1\",\n \"main\": \"/src/bubble-sort.ts\",\n \"devDependencies\": {\n \"@types/jest\": \"29.5.0\",\n \"typescript\": \"5.0.2\"\n }\n}\n",
|
||||
"/tsconfig.json": "{\n \"include\": [\"./**/*\"],\n \"compilerOptions\": {\n \"strict\": true,\n \"esModuleInterop\": true,\n \"lib\": [\"dom\", \"dom.iterable\", \"esnext\"],\n \"jsx\": \"react-jsx\",\n \"target\": \"es2020\"\n }\n}\n",
|
||||
"/src/bubble-sort.run.test.ts": "import bubbleSort from './bubble-sort';\n\ndescribe('bubbleSort', () => {\n test('empty', () => {\n expect(bubbleSort([])).toEqual([]);\n });\n\n test('one element', () => {\n expect(bubbleSort([1])).toEqual([1]);\n });\n\n test('two elements', () => {\n expect(bubbleSort([2, 1])).toEqual([1, 2]);\n expect(bubbleSort([1, 2])).toEqual([1, 2]);\n });\n});\n",
|
||||
"/src/bubble-sort.submit.test.ts": "import bubbleSort from './bubble-sort';\n\ndescribe('bubbleSort', () => {\n test('empty', () => {\n expect(bubbleSort([])).toEqual([]);\n });\n\n test('one element', () => {\n expect(bubbleSort([1])).toEqual([1]);\n });\n\n test('two elements', () => {\n expect(bubbleSort([2, 1])).toEqual([1, 2]);\n expect(bubbleSort([1, 2])).toEqual([1, 2]);\n });\n\n test('more than two elements', () => {\n expect(bubbleSort([10, 2, 4])).toEqual([2, 4, 10]);\n expect(bubbleSort([4, 5, 6, 1, 2, 3])).toEqual([1, 2, 3, 4, 5, 6]);\n expect(bubbleSort([1, 2, 3, 4, 5, 0])).toEqual([0, 1, 2, 3, 4, 5]);\n expect(bubbleSort([10, 9, 8, 7, 6, 5, 4, 3, 2, 1])).toEqual([\n 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,\n ]);\n expect(bubbleSort([5, 4, 3, 2, 1, 10, 9, 8, 7, 6])).toEqual([\n 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,\n ]);\n expect(bubbleSort([98322, 3242, 876, -234, 34, 12331])).toEqual([\n -234, 34, 876, 3242, 12331, 98322,\n ]);\n });\n\n test('duplicate elements', () => {\n expect(bubbleSort([1, 1])).toEqual([1, 1]);\n expect(bubbleSort([2, 2, 2])).toEqual([2, 2, 2]);\n expect(bubbleSort([2, 1, 2])).toEqual([1, 2, 2]);\n expect(bubbleSort([1, 1, 1, 1, 1, 1])).toEqual([1, 1, 1, 1, 1, 1]);\n expect(bubbleSort([7, 2, 4, 3, 1, 2])).toEqual([1, 2, 2, 3, 4, 7]);\n });\n});\n",
|
||||
"/src/bubble-sort.ts": "export default function bubbleSort(arr: Array<number>): Array<number> {\n // Do multiple iterations over the array.\n for (let i: number = 0; i < arr.length; i++) {\n // For each iteration, compare every adjacent pairs while ignoring the last i elements that are already sorted.\n for (let j: number = 0; j < arr.length - i; j++) {\n // If the left element in the pair is larger than the right one, swap their positions to ensure that elements are sorted ascendingly.\n if (arr[j] > arr[j + 1]) {\n let temp: number = arr[j];\n arr[j] = arr[j + 1];\n arr[j + 1] = temp;\n }\n }\n }\n\n // Return the sorted array.\n return arr;\n}\n"
|
||||
},
|
||||
"skeleton": {
|
||||
"js": "/**\n * @param {Array<number>} arr The input integer array to be sorted.\n * @return {Array<number>}\n */\nexport default function bubbleSort(arr) {\n throw 'Not implemented!';\n}",
|
||||
"ts": "export default function bubbleSort(arr: Array<number>): Array<number> {\n throw 'Not implemented!';\n}"
|
||||
},
|
||||
"workspace": {
|
||||
"main": "/src/bubble-sort.ts",
|
||||
"run": "/src/bubble-sort.run.test.ts",
|
||||
"submit": "/src/bubble-sort.submit.test.ts"
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1733616000,
|
||||
"difficulty": "medium",
|
||||
"duration": 40,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/coin-change",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "coin-change",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"dynamic-programming"
|
||||
]
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
27
apps/web/src/__generated__/questions/algo/combinations-target-sum/metadata.json
generated
Normal file
27
apps/web/src/__generated__/questions/algo/combinations-target-sum/metadata.json
generated
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1720310400,
|
||||
"difficulty": "medium",
|
||||
"duration": 40,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/combinations-target-sum",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "combinations-target-sum",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"dynamic-programming"
|
||||
]
|
||||
}
|
||||
20
apps/web/src/__generated__/questions/algo/combinations-target-sum/setup.json
generated
Normal file
20
apps/web/src/__generated__/questions/algo/combinations-target-sum/setup.json
generated
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
27
apps/web/src/__generated__/questions/algo/count-ones-in-binary/metadata.json
generated
Normal file
27
apps/web/src/__generated__/questions/algo/count-ones-in-binary/metadata.json
generated
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1718928000,
|
||||
"difficulty": "easy",
|
||||
"duration": 15,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/count-ones-in-binary",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "count-ones-in-binary",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"binary"
|
||||
]
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"access": "standard",
|
||||
"author": "hiten",
|
||||
"companies": [],
|
||||
"created": 1721260800,
|
||||
"difficulty": "medium",
|
||||
"duration": 40,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/course-dependency",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [],
|
||||
"slug": "course-dependency",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"graph",
|
||||
"topological-sort",
|
||||
"dfs"
|
||||
]
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,29 @@
|
|||
{
|
||||
"access": "free",
|
||||
"author": null,
|
||||
"companies": [],
|
||||
"created": 1671667200,
|
||||
"difficulty": "medium",
|
||||
"duration": 15,
|
||||
"featured": false,
|
||||
"format": "algo",
|
||||
"frameworkDefault": null,
|
||||
"frameworks": [],
|
||||
"href": "/questions/algo/depth-first-search",
|
||||
"importance": "low",
|
||||
"languages": [
|
||||
"js",
|
||||
"ts"
|
||||
],
|
||||
"nextQuestions": [],
|
||||
"published": true,
|
||||
"ranking": 100,
|
||||
"similarQuestions": [
|
||||
"breadth-first-search"
|
||||
],
|
||||
"slug": "depth-first-search",
|
||||
"subtitle": null,
|
||||
"topics": [
|
||||
"dfs"
|
||||
]
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
{
|
||||
"files": {
|
||||
"/package.json": "{\n \"name\": \"@gfe-questions/depth-first-search\",\n \"version\": \"0.0.1\",\n \"main\": \"/src/depth-first-search.ts\",\n \"devDependencies\": {\n \"@types/jest\": \"29.5.0\",\n \"typescript\": \"5.0.2\"\n }\n}\n",
|
||||
"/tsconfig.json": "{\n \"include\": [\"./**/*\"],\n \"compilerOptions\": {\n \"strict\": true,\n \"esModuleInterop\": true,\n \"lib\": [\"dom\", \"dom.iterable\", \"esnext\"],\n \"jsx\": \"react-jsx\",\n \"target\": \"es2020\"\n }\n}\n",
|
||||
"/src/depth-first-search.run.test.ts": "import depthFirstSearch from './depth-first-search';\n\ndescribe('depthFirstSearch', () => {\n test('empty graph', () => {\n expect(depthFirstSearch({}, 'A')).toEqual([]);\n });\n\n test('graphs with one node', () => {\n expect(depthFirstSearch({ A: [] }, 'A')).toEqual(['A']);\n });\n\n test('graphs with two nodes', () => {\n expect(depthFirstSearch({ A: ['B'], B: [] }, 'A')).toEqual(['A', 'B']);\n });\n});\n",
|
||||
"/src/depth-first-search.submit.test.ts": "import depthFirstSearch from './depth-first-search';\n\ndescribe('depthFirstSearch', () => {\n test('empty graph', () => {\n expect(depthFirstSearch({}, 'A')).toEqual([]);\n });\n\n test('graphs with one node', () => {\n expect(depthFirstSearch({ A: [] }, 'A')).toEqual(['A']);\n });\n\n test('graphs with two nodes', () => {\n expect(depthFirstSearch({ A: ['B'], B: [] }, 'A')).toEqual(['A', 'B']);\n expect(depthFirstSearch({ A: ['A', 'B'], B: [] }, 'A')).toEqual(['A', 'B']);\n expect(depthFirstSearch({ A: ['A', 'B'], B: [] }, 'B')).toEqual(['B']);\n expect(depthFirstSearch({ A: ['A', 'B'], B: ['A'] }, 'B')).toEqual([\n 'B',\n 'A',\n ]);\n });\n\n test('graphs with multiple nodes', () => {\n expect(depthFirstSearch({ A: ['B'], B: ['C'], C: [] }, 'A')).toEqual([\n 'A',\n 'B',\n 'C',\n ]);\n expect(depthFirstSearch({ A: ['B', 'C'], B: [], C: [] }, 'A')).toEqual([\n 'A',\n 'B',\n 'C',\n ]);\n expect(\n depthFirstSearch(\n { A: ['B', 'C'], B: [], C: [], D: ['B'], E: ['C'] },\n 'A',\n ),\n ).toEqual(['A', 'B', 'C']);\n expect(\n depthFirstSearch(\n { A: ['D', 'E'], B: [], C: [], D: ['B'], E: ['C'] },\n 'A',\n ),\n ).toEqual(['A', 'D', 'B', 'E', 'C']);\n expect(\n depthFirstSearch(\n {\n A: ['D', 'E'],\n B: ['A', 'B', 'C', 'D', 'E'],\n C: [],\n D: ['B'],\n E: ['C'],\n },\n 'A',\n ),\n ).toEqual(['A', 'D', 'B', 'C', 'E']);\n expect(\n depthFirstSearch(\n {\n A: ['A', 'B', 'C', 'D', 'E'],\n B: [],\n C: [],\n D: ['B'],\n E: ['C'],\n },\n 'A',\n ),\n ).toEqual(['A', 'B', 'C', 'D', 'E']);\n // Graph taken from https://www.geeksforgeeks.org/breadth-first-traversal-for-a-graph/\n const graph = {\n A: ['B', 'C'],\n B: ['A', 'D', 'E'],\n C: ['A', 'E'],\n D: ['B', 'E', 'F'],\n E: ['B', 'C', 'D', 'F'],\n F: ['D', 'E'],\n };\n expect(depthFirstSearch(graph, 'A')).toEqual([\n 'A',\n 'B',\n 'D',\n 'E',\n 'C',\n 'F',\n ]);\n expect(depthFirstSearch(graph, 'B')).toEqual([\n 'B',\n 'A',\n 'C',\n 'E',\n 'D',\n 'F',\n ]);\n expect(depthFirstSearch(graph, 'C')).toEqual([\n 'C',\n 'A',\n 'B',\n 'D',\n 'E',\n 'F',\n ]);\n expect(depthFirstSearch(graph, 'D')).toEqual([\n 'D',\n 'B',\n 'A',\n 'C',\n 'E',\n 'F',\n ]);\n expect(depthFirstSearch(graph, 'E')).toEqual([\n 'E',\n 'B',\n 'A',\n 'C',\n 'D',\n 'F',\n ]);\n expect(depthFirstSearch(graph, 'F')).toEqual([\n 'F',\n 'D',\n 'B',\n 'A',\n 'C',\n 'E',\n ]);\n });\n\n test('disjoint graphs', () => {\n expect(depthFirstSearch({ A: ['B'], B: [], C: [], D: ['C'] }, 'A')).toEqual(\n ['A', 'B'],\n );\n expect(depthFirstSearch({ A: ['B'], B: [], C: [], D: ['C'] }, 'C')).toEqual(\n ['C'],\n );\n expect(depthFirstSearch({ A: ['B'], B: [], C: [], D: ['C'] }, 'D')).toEqual(\n ['D', 'C'],\n );\n });\n\n test('cyclic graphs', () => {\n expect(depthFirstSearch({ A: ['A'] }, 'A')).toEqual(['A']);\n expect(\n depthFirstSearch(\n {\n A: ['B', 'C', 'D'],\n B: ['E', 'F'],\n C: ['G', 'H'],\n D: ['I', 'J'],\n E: ['D'],\n F: [],\n G: [],\n H: [],\n I: [],\n J: [],\n },\n 'A',\n ),\n ).toEqual(['A', 'B', 'E', 'D', 'I', 'J', 'F', 'C', 'G', 'H']);\n expect(\n depthFirstSearch(\n {\n A: ['B', 'C', 'D'],\n B: ['E', 'F'],\n C: ['G', 'H'],\n D: ['I', 'J'],\n E: ['D'],\n F: [],\n G: [],\n H: [],\n I: [],\n J: [],\n },\n 'B',\n ),\n ).toEqual(['B', 'E', 'D', 'I', 'J', 'F']);\n expect(\n depthFirstSearch(\n {\n A: ['B', 'C'],\n B: ['D', 'E'],\n C: ['F', 'G'],\n D: [],\n E: [],\n F: [],\n G: [],\n },\n 'A',\n ),\n ).toEqual(['A', 'B', 'D', 'E', 'C', 'F', 'G']);\n expect(\n depthFirstSearch(\n {\n A: ['B', 'C'],\n B: ['D', 'E'],\n C: ['F', 'G'],\n D: [],\n E: [],\n F: [],\n G: [],\n },\n 'E',\n ),\n ).toEqual(['E']);\n });\n});\n",
|
||||
"/src/depth-first-search.ts": "export default function depthFirstSearch(\n graph: Record<string, Array<string>>,\n source: string,\n): Array<string> {\n // If there are no nodes in the graph, just return an empty array\n if (Object.keys(graph).length === 0) {\n return [];\n }\n\n // Create an stack to store the nodes to be visited. We can simulate\n // stacks using arrays in JavaScript.\n // Add the root node since we're doing a pre-order DFS.\n const toBeVisited: Array<string> = [];\n toBeVisited.push(source);\n\n // Initialize a set that tracks visited nodes.\n const visited = new Set<string>();\n\n // Loop as long as array is empty (i.e. there are still nodes to be visited).\n while (toBeVisited.length !== 0) {\n // Pop top node from array (toBeVisited) and add it to the set (visited).\n const node = toBeVisited.pop()!;\n visited.add(node);\n\n // Retrieve neighbors (values of the adjacency list input Object)\n const neighbors = graph[node];\n // Push neighbors, in reverse order, onto array to be visited\n // to preserve original order of neighbors when visiting (popping off the array).\n for (let i = neighbors.length - 1; i >= 0; i--) {\n const neighbor = neighbors[i];\n // First check if the neighbor has already been visited before adding it.\n if (!visited.has(neighbor)) {\n toBeVisited.push(neighbor);\n }\n }\n }\n\n // The visited nodes is the traversal order.\n return Array.from(visited);\n}\n"
|
||||
},
|
||||
"skeleton": {
|
||||
"js": "/**\n * @param {Record<string, Array<string>} graph The adjacency list representing the graph.\n * @param {string} source The source node to start traversal from. It has to exist as a node in the graph.\n * @return {Array<string>} A DFS-traversed order of nodes.\n */\nexport default function depthFirstSearch(graph, source) {\n throw 'Not implemented!';\n}",
|
||||
"ts": "export default function depthFirstSearch(\n graph: Record<string, Array<string>>,\n source: string,\n): Array<string> {\n throw 'Not implemented!';\n}"
|
||||
},
|
||||
"workspace": {
|
||||
"main": "/src/depth-first-search.ts",
|
||||
"run": "/src/depth-first-search.run.test.ts",
|
||||
"submit": "/src/depth-first-search.submit.test.ts"
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue