fix(guide): restructure curriculum guide articles (#36501)

* fix: restructure certifications guide articles
* fix: added 3 dashes line before prob expl
* fix: added 3 dashes line before hints
* fix: added 3 dashes line before solutions
This commit is contained in:
Randell Dawson
2019-07-24 00:59:27 -07:00
committed by mrugesh
parent c911e77eed
commit 1494a50123
990 changed files with 13202 additions and 8628 deletions

View File

@@ -1,7 +1,7 @@
---
title: Add a New Element to a Binary Search Tree
---
## Add a New Element to a Binary Search Tree
# Add a New Element to a Binary Search Tree
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/add-a-new-element-to-a-binary-search-tree/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Add Elements at a Specific Index in a Linked List
---
## Add Elements at a Specific Index in a Linked List
# Add Elements at a Specific Index in a Linked List
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/add-elements-at-a-specific-index-in-a-linked-list/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,50 +1,44 @@
---
title: Adjacency List
---
## Adjacency List
![:triangular_flag_on_post:](https://forum.freecodecamp.com/images/emoji/emoji_one/triangular_flag_on_post.png?v=3 ":triangular_flag_on_post:") Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program ![:busts_in_silhouette:](https://forum.freecodecamp.com/images/emoji/emoji_one/busts_in_silhouette.png?v=3 ":busts_in_silhouette:") and write your own code ![:pencil:](https://forum.freecodecamp.com/images/emoji/emoji_one/pencil.png?v=3 ":pencil:")
# Adjacency List
### ![:checkered_flag:](https://forum.freecodecamp.com/images/emoji/emoji_one/checkered_flag.png?v=3 ":checkered_flag:") Problem Explanation:
---
## Problem Explanation
To solve this problem, you have to create a Javascript Object to emulate an undirected graph in the form of an adjacency list.
## ![:speech_balloon:](https://forum.freecodecamp.com/images/emoji/emoji_one/speech_balloon.png?v=3 ":speech_balloon:") Hint: 1
---
## Hints
### Hint 1
Create keys with the names James, Jill, Jenny and Jeff.
> _try to solve the problem now_
## ![:speech_balloon:](https://forum.freecodecamp.com/images/emoji/emoji_one/speech_balloon.png?v=3 ":speech_balloon:") Hint: 2
### Hint 2
Read the presentation and try to understand what it means to be an undirected graph.
> _try to solve the problem now_
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
```js
var undirectedAdjList = {
James: ["Jeff"],
Jill: ["Jenny"],
Jenny: ["Jill", "Jeff"],
Jeff: ["Jenny", "James"]
};
```
## Spoiler Alert!
![warning sign](//discourse-user-assets.s3.amazonaws.com/original/2X/2/2d6c412a50797771301e7ceabd554cef4edcd74d.gif)
**Solution ahead!**
## ![:beginner:](https://forum.freecodecamp.com/images/emoji/emoji_one/beginner.png?v=3 ":beginner:") Basic Code Solution:
var undirectedAdjList = {
James: ["Jeff"],
Jill: ["Jenny"],
Jenny: ["Jill", "Jeff"],
Jeff: ["Jenny", "James"]
};
### Code Explanation:
#### Code Explanation
* The undirected graph is created using a Javascript Object. Each unique name is a key and the each person who has a relationship with the name is in the unique name's array value. e.g. if James and Jeff have a relationship, Jeff will be in James's array value and James will be in Jeff's array value.
## ![:clipboard:](https://forum.freecodecamp.com/images/emoji/emoji_one/clipboard.png?v=3 ":clipboard:") NOTES FOR CONTRIBUTIONS:
* ![:warning:](https://forum.freecodecamp.com/images/emoji/emoji_one/warning.png?v=3 ":warning:") **DO NOT** add solutions that are similar to any existing solutions. If you think it is **_similar but better_**, then try to merge (or replace) the existing similar solution.
* Add an explanation of your solution.
* Categorize the solution in one of the following categories — **Basic**, **Intermediate** and **Advanced**. ![:traffic_light:](https://forum.freecodecamp.com/images/emoji/emoji_one/traffic_light.png?v=3 ":traffic_light:")
</details>

View File

@@ -1,7 +1,7 @@
---
title: Adjacency Matrix
---
## Adjacency Matrix
# Adjacency Matrix
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/adjacency-matrix/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,56 +1,46 @@
---
title: Breadth-First Search
---
## Breadth-First Search
# Breadth-First Search
Let's first define the `Tree` class to be used for the implementation of the Breadth First Search algorithm.
```python
class Tree:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
```js
function bfs(graph, root) {
// Distance object returned
var nodesLen = {};
// Set all distances to infinity
for (var i = 0; i < graph.length; i++) {
nodesLen[i] = Infinity;
}
nodesLen[root] = 0; // ...except root node
var queue = [root]; // Keep track of nodes to visit
var current; // Current node traversing
// Keep on going until no more nodes to traverse
while (queue.length !== 0) {
current = queue.shift();
// Get adjacent nodes from current node
var curConnected = graph[current]; // Get layer of edges from current
var neighborIdx = []; // List of nodes with edges
var idx = curConnected.indexOf(1); // Get first edge connection
while (idx !== -1) {
neighborIdx.push(idx); // Add to list of neighbors
idx = curConnected.indexOf(1, idx + 1); // Keep on searching
}
// Loop through neighbors and get lengths
for (var j = 0; j < neighborIdx.length; j++) {
// Increment distance for nodes traversed
if (nodesLen[neighborIdx[j]] === Infinity) {
nodesLen[neighborIdx[j]] = nodesLen[current] + 1;
queue.push(neighborIdx[j]); // Add new neighbors to queue
}
}
}
return nodesLen;
}
```
The breadth first search algorithm moves from one level to another starting from the root of the tree. We will make use of a `queue` for this.
```python
def bfs(root_node):
queue = [root_node]
while queue:
top_element = queue.pop()
print("Node processed: ",top_element)
if top_element.left:
queue.append(top_element.left)
if top_element.right:
queue.append(top_element.right)
```
We can easily modify the above code to print the level of each node as well.
```python
def bfs(root_node):
queue = [(root_node, 0)]
while queue:
top_element, level = queue.pop()
print("Node processed: {} at level {}".format(top_element, level))
if top_element.left:
queue.append((top_element.left, level + 1))
if top_element.right:
queue.append((top_element.right, level + 1))
```
| Complexity | Time | Space |
| ----- | ------ | ------ |
| BFS | n | n |
</details>

View File

@@ -1,7 +1,7 @@
---
title: Check if an Element is Present in a Binary Search Tree
---
## Check if an Element is Present in a Binary Search Tree
# Check if an Element is Present in a Binary Search Tree
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/check-if-an-element-is-present-in-a-binary-search-tree/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,81 +1,88 @@
---
title: Create a Circular Queue
---
## Create a Circular Queue
# Create a Circular Queue
### Method:
---
## Problem Explanation
- In this challenge we create a Circular Queue data structure.
- First, we need to create an array of `size` with all elements set to `null`.
- Then we create an equeue method, which moves the write pointer but doesnt exceed the read pointer.
- The dequeue method on the other hand, moves the read pointer but doesnt exceed the write pointer.
- Example:
- First, we create an array of length 5:
```shell
```
[null, null, null, null, null]
^Read @ 0
^Write @ 0
```
- Then we enqueue `a`, `b`, and `c`:
```shell
```
- Then we enqueue `a`, `b`, and `c`:
```
[a, b, c, null, null]
^Read @ 0
^Write @ 3
```
```
- Now we dequeue all the enqueued items:
```shell
```
[null, null, null, null, null]
^Read @ 3
^Write @ 3
```
```
- Finally, we enqueue `d`, `e` and `f`:
```shell
```
[f, null, null, d, e]
^Read @ 3
^Write @ 1
```
### Solution:
```
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
```js
class CircularQueue {
constructor(size) {
this.queue = [];
this.read = 0;
this.write = 0;
this.max = size - 1;
while (size > 0) {
this.queue.push(null);
size--;
}
constructor(size) {
this.queue = [];
this.read = 0;
this.write = 0;
this.max = size - 1;
while (size > 0) {
this.queue.push(null);
size--;
}
print() {
return this.queue;
}
print() {
return this.queue;
}
enqueue(item) {
if (this.queue[this.write] === null) {
this.queue[this.write++] = item;
if (this.write > this.max) this.write = 0;
return item;
}
enqueue(item) {
if (this.queue[this.write] === null){
this.queue[this.write++] = item;
if (this.write > this.max) this.write = 0;
return item;
}
return null;
return null;
}
dequeue() {
if (this.queue[this.read] != null) {
let item = this.queue[this.read];
this.queue[this.read++] = null;
if (this.read > this.max) this.read = 0;
return item;
}
dequeue() {
if (this.queue[this.read] != null){
let item = this.queue[this.read];
this.queue[this.read++] = null;
if (this.read > this.max) this.read = 0;
return item;
}
return null;
}
}
```
### References:
return null;
}
}
```
#### Relevant Links
- [Wikipedia](https://en.wikipedia.org/wiki/Circular_buffer)
</details>

View File

@@ -1,7 +1,7 @@
---
title: Create a Doubly Linked List
---
## Create a Doubly Linked List
# Create a Doubly Linked List
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/create-a-doubly-linked-list/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Create a Hash Table
---
## Create a Hash Table
# Create a Hash Table
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/create-a-hash-table/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Create a Linked List Class
---
## Create a Linked List Class
# Create a Linked List Class
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/create-a-linked-list-class/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Create a Map Data Structure
---
## Create a Map Data Structure
# Create a Map Data Structure
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/create-a-map-data-structure/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,9 +1,10 @@
---
title: Create a Priority Queue Class
---
## Create a Priority Queue Class
# Create a Priority Queue Class
### Method:
---
## Problem Explanation
- Priority Queue is an Abstract Data Type.
- It can be implemented using other Data Structures but is commonly implemented using a Heap.
- Each node contains a priority. When we enqueue a node to the queue, it's "bubbled up" to its place in the queue.
@@ -18,7 +19,11 @@ title: Create a Priority Queue Class
| Priority Queue | 1 | 1 | logn | logn |
### Solution:
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
```js
//function which swaps elements in array, using ES6 syntax
function swap(arr, i, j) {
@@ -121,5 +126,7 @@ class PriorityQueue {
}
```
### References:
#### Relevant Links
- [Wikipedia](https://en.wikipedia.org/wiki/Priority_queue)
</details>

View File

@@ -1,10 +1,11 @@
---
title: Create a Queue Class
---
## Create a Queue Class
# Create a Queue Class
### Method:
---
## Problem Explanation
- A Queue is an abstract Data Structure.
- A Queue folow FIFO/LILO principle.
- In this challenge we nede to implement `enqueue()`, `dequeue()`, `front()`, `size()`, `isEmpty()` methods.
@@ -20,61 +21,69 @@ title: Create a Queue Class
- ![Queue in action](https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Data_Queue.svg/405px-Data_Queue.svg.png)
### Solution:
#### Basic:
##### Note:
- This solution is not exactly a queue, the shift() method used in the dequeue() method is of complexity `O(n)` and not `O(1)`. However, the advanced solution rectifies this and uses Object(HashTables) instead of Array to implement Queue.
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
**Note:** This solution is not exactly a queue, the shift() method used in the dequeue() method is of complexity `O(n)` and not `O(1)`. However, the advanced solution rectifies this and uses Object(HashTables) instead of Array to implement Queue.
```js
function Queue () {
var collection = [];
this.print = function() {
console.log(collection);
};
this.enqueue = function(val){
collection.push(val);
};
this.dequeue = function(){
return collection.shift();
}
this.front = function(){
return collection[0];
}
this.size = function(){
return collection.length;
}
this.isEmpty = function(){
return collection.length === 0;
}
function Queue() {
var collection = [];
this.print = function() {
console.log(collection);
};
this.enqueue = function(val) {
collection.push(val);
};
this.dequeue = function() {
return collection.shift();
};
this.front = function() {
return collection[0];
};
this.size = function() {
return collection.length;
};
this.isEmpty = function() {
return collection.length === 0;
};
}
```
#### Advanced - ES6 class syntax:
</details>
<details><summary>Solution 2 (Click to Show/Hide)</summary>
```js
class Queue {
constructor(){
this.collection = {};
this.start = 0;
this.end = 0;
}
print(){
console.log(this.collection);
}
enqueue(val){
this.collection[this.end++] = val;
}
dequeue(){
return this.collection[this.start++];
}
front(){
return this.collection[this.start];
}
size(){
return this.end - this.start;
}
isEmpty(){
return this.size() === 0;
}
constructor() {
this.collection = {};
this.start = 0;
this.end = 0;
}
print() {
console.log(this.collection);
}
enqueue(val) {
this.collection[this.end++] = val;
}
dequeue() {
return this.collection[this.start++];
}
front() {
return this.collection[this.start];
}
size() {
return this.end - this.start;
}
isEmpty() {
return this.size() === 0;
}
}
```
### References:
#### Relevant Links
- [Wikipedia](https://en.wikipedia.org/wiki/Queue_(abstract_data_type))
</details>

View File

@@ -1,32 +1,39 @@
---
title: Create a Set Class
---
## Create a Set Class
# Create a Set Class
### Method:
---
## Problem Explanation
- A Set is an abstract data structure.
- It can store unique value and the collection is unordered.
- In this challenge, we have to implement `.add()` method. This method should only add unique values to `collection`.
- The method should return `true`, if the value is sucessfully added to the collection, otherwise it should return `false`.
### Solution:
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
```js
function Set() {
// the var collection will hold our set
var collection = [];
// this method will check for the presence of an element and return true or false
this.has = function(element) {
return (collection.indexOf(element) !== -1);
};
// this method will return all the values in the set
this.values = function() {
return collection;
};
this.add = function(el) {
return this.has(el) ? false : Boolean(collection.push(el));
}
// the var collection will hold our set
var collection = [];
// this method will check for the presence of an element and return true or false
this.has = function(element) {
return collection.indexOf(element) !== -1;
};
// this method will return all the values in the set
this.values = function() {
return collection;
};
this.add = function(el) {
return this.has(el) ? false : Boolean(collection.push(el));
};
}
```
### Resources:
#### Relevant Links
- [Wikipedia](https://en.wikipedia.org/wiki/Set_(abstract_data_type))
</details>

View File

@@ -1,9 +1,10 @@
---
title: Create a Stack Class
---
## Create a Stack Class
# Create a Stack Class
### Method:
---
## Problem Explanation
- Stack is an abstract data structure.
- Stack follows LIFO/FILO principle.
- In this challenge, we need to add `.push()`, `.pop()`, `.peek()`, `.isEmpty()` and `.clear()` methods to the class.
@@ -18,58 +19,65 @@ title: Create a Stack Class
| ----- | ------ | ------ | ------ | ------ |
| Stack | n | n | 1 | 1 |
### Solution:
#### Basic:
```js
function Stack() {
var collection = [];
this.print = function() {
console.log(collection);
};
this.push = function(val){
return collection.push(val);
}
this.pop = function(){
return collection.pop();
}
this.peek = function(){
return collection[collection.length-1];
}
this.isEmpty = function(){
return collection.length === 0;
}
this.clear = function(){
collection.length = 0;
}
}
```
#### Advanced - ES6 Class syntax:
```js
class Stack {
constructor() {
this.collection = [];
}
print(){
console.log(this.collection);
}
push(val){
retiurn this.collection.push(val);
}
pop(){
return this.collection.pop();
}
peek(){
return this.collection[this.collection.length-1];
}
isEmpty(){
return this.collection.length === 0;
}
clear(){
return this.collection.length = 0;
}
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
```js
function Stack() {
var collection = [];
this.print = function() {
console.log(collection);
};
this.push = function(val) {
return collection.push(val);
};
this.pop = function() {
return collection.pop();
};
this.peek = function() {
return collection[collection.length - 1];
};
this.isEmpty = function() {
return collection.length === 0;
};
this.clear = function() {
collection.length = 0;
};
}
```
### Resources:
</details>
<details><summary>Solution 2 (Click to Show/Hide)</summary>
```js
class Stack {
constructor() {
this.collection = [];
}
print() {
console.log(this.collection);
}
push(val) {
return this.collection.push(val);
}
pop() {
return this.collection.pop();
}
peek() {
return this.collection[this.collection.length - 1];
}
isEmpty() {
return this.collection.length === 0;
}
clear() {
return (this.collection.length = 0);
}
}
```
#### Relevant Links
- [Wikipedia](https://en.wikipedia.org/wiki/Stack_(abstract_data_type))
</details>

View File

@@ -1,7 +1,7 @@
---
title: Create a Trie Search Tree
---
## Create a Trie Search Tree
# Create a Trie Search Tree
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/create-a-trie-search-tree/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Create an ES6 JavaScript Map
---
## Create an ES6 JavaScript Map
# Create an ES6 JavaScript Map
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/create-an-es6-javascript-map/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,37 +1,31 @@
---
title: Create and Add to Sets in ES6
---
## Create and Add to Sets in ES6
# Create and Add to Sets in ES6
![:triangular_flag_on_post:](https://forum.freecodecamp.com/images/emoji/emoji_one/triangular_flag_on_post.png?v=3 ":triangular_flag_on_post:") Remember to use <a>**`Read-Search-Ask`**</a> if you get stuck. Try to pair program ![:busts_in_silhouette:](https://forum.freecodecamp.com/images/emoji/emoji_one/busts_in_silhouette.png?v=3 ":busts_in_silhouette:") and write your own code ![:pencil:](https://forum.freecodecamp.com/images/emoji/emoji_one/pencil.png?v=3 ":pencil:")
### ![:checkered_flag:](https://forum.freecodecamp.com/images/emoji/emoji_one/checkered_flag.png?v=3 ":checkered_flag:") Problem Explanation:
---
## Problem Explanation
To solve this problem, you have to add an array of items to the set.
## ![:speech_balloon:](https://forum.freecodecamp.com/images/emoji/emoji_one/speech_balloon.png?v=3 ":speech_balloon:") Hint: 1
---
## Hints
### Hint 1
Use the add function to add an array of strings to the set.
> _try to solve the problem now_
## ![:speech_balloon:](https://forum.freecodecamp.com/images/emoji/emoji_one/speech_balloon.png?v=3 ":speech_balloon:") Hint: 2
### Hint 2
Use the length attribute on the values of the Set.
> _try to solve the problem now_
---
## Solutions
## Spoiler Alert!
![warning sign](//discourse-user-assets.s3.amazonaws.com/original/2X/2/2d6c412a50797771301e7ceabd554cef4edcd74d.gif)
**Solution ahead!**
## ![:beginner:](https://forum.freecodecamp.com/images/emoji/emoji_one/beginner.png?v=3 ":beginner:") Basic Code Solution:
<details><summary>Solution 1 (Click to Show/Hide)</summary>
```js
function checkSet() {
@@ -46,13 +40,10 @@ function checkSet() {
checkSet();
```
### Code Explanation:
#### Code Explanation
* Creating a set object as shown in pre-written code will create the set without duplicate objects.
* Therefore, by using the add function, we can add items to the set and they will not be duplicated, and will still be represented in the array.
## ![:clipboard:](https://forum.freecodecamp.com/images/emoji/emoji_one/clipboard.png?v=3 ":clipboard:") NOTES FOR CONTRIBUTIONS:
* ![:warning:](https://forum.freecodecamp.com/images/emoji/emoji_one/warning.png?v=3 ":warning:") **DO NOT** add solutions that are similar to any existing solutions. If you think it is **_similar but better_**, then try to merge (or replace) the existing similar solution.
* Add an explanation of your solution.
* Categorize the solution in one of the following categories — **Basic**, **Intermediate** and **Advanced**. ![:traffic_light:](https://forum.freecodecamp.com/images/emoji/emoji_one/traffic_light.png?v=3 ":traffic_light:")
</details>

View File

@@ -1,7 +1,7 @@
---
title: Delete a Leaf Node in a Binary Search Tree
---
## Delete a Leaf Node in a Binary Search Tree
# Delete a Leaf Node in a Binary Search Tree
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/delete-a-leaf-node-in-a-binary-search-tree/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Delete a Node with One Child in a Binary Search Tree
---
## Delete a Node with One Child in a Binary Search Tree
# Delete a Node with One Child in a Binary Search Tree
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/delete-a-node-with-one-child-in-a-binary-search-tree/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Delete a Node with Two Children in a Binary Search Tree
---
## Delete a Node with Two Children in a Binary Search Tree
# Delete a Node with Two Children in a Binary Search Tree
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/delete-a-node-with-two-children-in-a-binary-search-tree/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Depth-First Search
---
## Depth-First Search
# Depth-First Search
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/depth-first-search/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,87 +1,88 @@
---
title: Find the Minimum and Maximum Height of a Binary Search Tree
---
## Find the Minimum and Maximum Height of a Binary Search Tree
# Find the Minimum and Maximum Height of a Binary Search Tree
### Solution
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
```js
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
this.value = value;
this.left = null;
this.right = null;
this.value = value;
this.left = null;
this.right = null;
}
function BinarySearchTree() {
this.root = null;
// change code below this line
// change code above this line
this.findMinHeight = function(root = this.root) {
// empty tree.
if(root === null) {
return -1;
}
// leaf node.
if(root.left === null && root.right === null) {
return 0;
}
if(root.left === null){
return this.findMinHeight(root.right) + 1;
}
if(root.right === null){
return this.findMinHeight(root.left) + 1;
}
const lHeight = this.findMinHeight(root.left);
const rHeight = this.findMinHeight(root.right);
return Math.min(lHeight, rHeight) + 1;
};
this.findMaxHeight = function(root = this.root) {
// empty tree.
if(root === null) {
return -1;
}
// leaf node.
if(root.left === null && root.right === null) {
return 0;
}
if(root.left === null){
return this.findMaxHeight(root.right) + 1;
}
if(root.right === null){
return this.findMaxHeight(root.left) + 1;
}
const lHeight = this.findMaxHeight(root.left);
const rHeight = this.findMaxHeight(root.right);
return Math.max(lHeight, rHeight) + 1;
};
this.isBalanced = function(root = this.root) {
this.root = null;
// change code below this line
// change code above this line
this.findMinHeight = function(root = this.root) {
// empty tree.
if (root === null) {
return -1;
}
// leaf node.
if (root.left === null && root.right === null) {
return 0;
}
if (root.left === null) {
return this.findMinHeight(root.right) + 1;
}
if (root.right === null) {
return this.findMinHeight(root.left) + 1;
}
const lHeight = this.findMinHeight(root.left);
const rHeight = this.findMinHeight(root.right);
return Math.min(lHeight, rHeight) + 1;
};
this.findMaxHeight = function(root = this.root) {
// empty tree.
if (root === null) {
return -1;
}
// leaf node.
if (root.left === null && root.right === null) {
return 0;
}
if (root.left === null) {
return this.findMaxHeight(root.right) + 1;
}
if (root.right === null) {
return this.findMaxHeight(root.left) + 1;
}
const lHeight = this.findMaxHeight(root.left);
const rHeight = this.findMaxHeight(root.right);
return Math.max(lHeight, rHeight) + 1;
};
this.isBalanced = function(root = this.root) {
if (root === null) {
return true;
}
if(root === null) {
return true;
}
if (root.left === null && root.right === null) {
return true;
}
if(root.left === null && root.right === null){
return true;
}
if (root.left === null) {
return this.findMaxHeight(root.right) <= 0;
}
if(root.left === null) {
return this.findMaxHeight(root.right) <= 0;
}
if (root.right === null) {
return this.findMaxHeight(root.left) <= 0;
}
if(root.right === null) {
return this.findMaxHeight(root.left) <= 0;
}
const lHeight = this.findMaxHeight(root.left);
const rHeight = this.findMaxHeight(root.right);
if(Math.abs(lHeight - rHeight) > 1){
return false;
}
return this.isBalanced(root.left) && this.isBalanced(root.right);
};
const lHeight = this.findMaxHeight(root.left);
const rHeight = this.findMaxHeight(root.right);
if (Math.abs(lHeight - rHeight) > 1) {
return false;
}
return this.isBalanced(root.left) && this.isBalanced(root.right);
};
}
```
</details>

View File

@@ -1,7 +1,7 @@
---
title: Find the Minimum and Maximum Value in a Binary Search Tree
---
## Find the Minimum and Maximum Value in a Binary Search Tree
# Find the Minimum and Maximum Value in a Binary Search Tree
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/find-the-minimum-and-maximum-value-in-a-binary-search-tree/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Implement Heap Sort with a Min Heap
---
## Implement Heap Sort with a Min Heap
# Implement Heap Sort with a Min Heap
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/implement-heap-sort-with-a-min-heap/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Incidence Matrix
---
## Incidence Matrix
# Incidence Matrix
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/incidence-matrix/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Data Structures
---
## Data Structures
# Data Structures
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/mathematics/quadratic-equations/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Insert an Element into a Max Heap
---
## Insert an Element into a Max Heap
# Insert an Element into a Max Heap
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/insert-an-element-into-a-max-heap/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,33 +1,28 @@
---
title: Invert a Binary Tree
---
## Invert a Binary Tree
# Invert a Binary Tree
---
## Hints
## Hint: 1
### Hint 1
Create a invert(node = this.root) method in the BinarySearchTree constructor function.
> _try to solve the problem now_
## Hint: 2
Try to use recursion and think of a base case.
> _try to solve the problem now_
---
## Solutions
## Spoiler Alert!
**Solution ahead!**
## Basic Code Solution:
<details><summary>Solution 1 (Click to Show/Hide)</summary>
```js
var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2));
var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
this.value = value;
this.left = null;
@@ -45,15 +40,13 @@ function BinarySearchTree() {
this.invert(node.right);
}
return node;
}
// change code above this line
};
// change code above this line
}
```
<a href='https://repl.it/repls/SereneScholarlyAnalyst' target='_blank' rel='nofollow'>Run Code</a>
### Code Explanation:
#### Code Explanation
* Using recursion will allow you to traverse each node once and the only extra memory used is the auxiliary temp variable that enables you to swap. You keep swapping the left and right pointers of a node until you reach the leaves which will not do anything as the left and right of them are null references.
## NOTES FOR CONTRIBUTIONS:
**DO NOT** add solutions that are similar to any existing solutions. If you think it is **_similar but better_**, then try to merge (or replace) the existing similar solution.
* Add an explanation of your solution.
* Categorize the solution in one of the following categories — **Basic**, **Intermediate** and **Advanced**.
</details>

View File

@@ -1,23 +1,30 @@
---
title: Learn how a Stack Works
---
## Learn how a Stack Works
# Learn how a Stack Works
### Method:
---
## Problem Explanation
- Stacks are an abstract data structures.
- They follow LIFO (Last In First Out) or FILO (First In Last Out) principle.
- Stack's insertion and deletion operations are of **O(1)** time complexity.
- In JavaScript, arrays can be treated as a Stack since `.push()` and `.pop()` methods have time complexity of **O(1)**.
- In this challenge we need to `.pop()` and then `.push()` into the stack.
### Solution:
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
```js
var homeworkStack = ["BIO12","HIS80","MAT122","PSY44"];
var homeworkStack = ["BIO12", "HIS80", "MAT122", "PSY44"];
homeworkStack.pop();
homeworkStack.push("CS50");
```
### Reference:
#### Relevant Links
- [Wikipedia](https://en.wikipedia.org/wiki/Stack_(abstract_data_type))
- Video by [Hackerrank](https://www.youtube.com/watch?v=wjI1WNcIntg)
</details>

View File

@@ -1,7 +1,7 @@
---
title: Perform a Difference on Two Sets of Data
---
## Perform a Difference on Two Sets of Data
# Perform a Difference on Two Sets of Data
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/perform-a-difference-on-two-sets-of-data/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Perform a Subset Check on Two Sets of Data
---
## Perform a Subset Check on Two Sets of Data
# Perform a Subset Check on Two Sets of Data
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/perform-a-subset-check-on-two-sets-of-data/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Perform a Union on Two Sets
---
## Perform a Union on Two Sets
# Perform a Union on Two Sets
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/perform-a-union-on-two-sets/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Perform an Intersection on Two Sets of Data
---
## Perform an Intersection on Two Sets of Data
# Perform an Intersection on Two Sets of Data
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/perform-an-intersection-on-two-sets-of-data/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Remove an Element from a Max Heap
---
## Remove an Element from a Max Heap
# Remove an Element from a Max Heap
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/remove-an-element-from-a-max-heap/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Remove Elements from a Linked List by Index
---
## Remove Elements from a Linked List by Index
# Remove Elements from a Linked List by Index
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/remove-elements-from-a-linked-list-by-index/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Remove Elements from a Linked List
---
## Remove Elements from a Linked List
# Remove Elements from a Linked List
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/remove-elements-from-a-linked-list/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Remove items from a set in ES6
---
## Remove items from a set in ES6
# Remove items from a set in ES6
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/remove-items-from-a-set-in-es6/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -2,15 +2,20 @@
title: Reverse a Doubly Linked List
---
## Reverse a Doubly Linked List
# Reverse a Doubly Linked List
### Method:
---
## Problem Explanation
- Reverse the doubly linked list, and update previous and next variables for each member node accordingly.
- Define privileged methods add() and reverse().
- add() will find the end of the list and append new entries at this location.
- reverse() will swap entries one pair at a time using a temporary variable.
### Solution:
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
```js
var Node = function(data, prev) {
@@ -31,32 +36,32 @@ var DoublyLinkedList = function() {
this.head = node;
this.tail = node;
} else {
while (currentNode.next) {
previousNode = currentNode;
while (currentNode.next) {
previousNode = currentNode;
currentNode = currentNode.next;
}
node.prev = currentNode;
currentNode.next = node;
this.tail = node;
}
};
};
this.reverse = function() {
let temp = null;
let currentNode = this.head;
if (this.head === null) {
return null;
}
this.tail = currentNode;
while (currentNode) {
temp = currentNode.prev;
temp = currentNode.prev;
currentNode.prev = currentNode.next;
currentNode.next = temp;
currentNode = currentNode.prev;
}
if (temp != null) {
this.head = temp.prev;
}
@@ -65,5 +70,7 @@ var DoublyLinkedList = function() {
};
```
### Reference:
#### Relevant Links
- [Wikipedia](https://en.wikipedia.org/wiki/Doubly_linked_list)
</details>

View File

@@ -1,7 +1,7 @@
---
title: Search within a Linked List
---
## Search within a Linked List
# Search within a Linked List
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/search-within-a-linked-list/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,13 +1,19 @@
---
title: Typed Arrays
---
## Typed Arrays
# Typed Arrays
### Method:
---
## Problem Explanation
- In this challenge, first we need to create a buffer of 64 bytes. We can use `ArrayBuffer()` constructor.
- After creating a buffer we need to create an Int32Array, for that we can use `Int32Array()` constructor.
### Solution:
---
## Solutions
<details><summary>Solution 1 (Click to Show/Hide)</summary>
```js
//Create a buffer of 64 bytes
var buffer = new ArrayBuffer(64);
@@ -16,6 +22,8 @@ var buffer = new ArrayBuffer(64);
var i32View = new Int32Array(buffer);
```
### References:
#### Relevant Links
- [ArrayBuffer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer)
- [TypedArray](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays)
</details>

View File

@@ -1,7 +1,7 @@
---
title: Use .has and .size on an ES6 Set
---
## Use .has and .size on an ES6 Set
# Use .has and .size on an ES6 Set
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/use-.has-and-.size-on-an-es6-set/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Use Breadth First Search in a Binary Search Tree
---
## Use Breadth First Search in a Binary Search Tree
# Use Breadth First Search in a Binary Search Tree
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/use-breadth-first-search-in-a-binary-search-tree/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Use Depth First Search in a Binary Search Tree
---
## Use Depth First Search in a Binary Search Tree
# Use Depth First Search in a Binary Search Tree
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/use-depth-first-search-in-a-binary-search-tree/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Use Spread and Notes for ES5 Set() Integration
---
## Use Spread and Notes for ES5 Set() Integration
# Use Spread and Notes for ES5 Set() Integration
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/use-spread-and-notes-for-es5-set-integration/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.

View File

@@ -1,7 +1,7 @@
---
title: Work with Nodes in a Linked List
---
## Work with Nodes in a Linked List
# Work with Nodes in a Linked List
This is a stub. <a href='https://github.com/freecodecamp/guides/tree/master/src/pages/certifications/coding-interview-prep/data-structures/work-with-nodes-in-a-linked-list/index.md' target='_blank' rel='nofollow'>Help our community expand it</a>.