ds-algo-study
Version:
Just experimenting with publishing a package
823 lines • 251 kB
HTML
<h2 id="binary-trees">Binary Trees</h2>
<ol type="1">
<li>Explain and implement a Binary Tree.</li>
</ol>
<ul>
<li>A tree is a collection of nodes and edges between them.</li>
<li>It cannot have any cycles, which are edges that form a loop between nodes.</li>
<li>We also only consider rooted trees in computer science, which is a tree that has one root node that is able to
access all other nodes.</li>
<li>For a tree to be a binary tree, each node can have a maximum of two children.</li>
<li>It's important to be able to identify and explain tree terminology as well. If given a tree, be able to point out
each component.
<ul>
<li>root: The single node of a tree that can access every other node through edges.</li>
<li>parent node: A node that is connected to lower nodes in the tree. If a tree only has one node, it is not a
parent node because there are no children.</li>
<li>child node: A node that is connected to a higher node in the tree. Every node except for the root is a child
node of some parent.</li>
<li>sibling nodes: Nodes that have the same parent.</li>
<li>leaf node: A node that has no children (at the ends of the branches of the tree)</li>
<li>internal node: A non-leaf node (aka a parent)</li>
<li>path: A series of nodes that can be traveled through edges.</li>
<li>subtree: A smaller portion of the original tree. Any node that is not the root node is itself the root of a
subtree.</li>
</ul>
</li>
<li>Know the basics of each term
<ul>
<li>A non-empty tree has to have a root.</li>
<li>A tree doesn't have any parent nodes if there are no children.</li>
<li>What's the min/max number of parent and leaf nodes for a tree with 5 nodes?
<ul>
<li>Two extreme implementations:</li>
</ul>
<p>[min-max-nodes-ll.png]</p>
<ul>
<li>Implementing in a chain results in max number of parents and min number of leaves: 4 parents, 1 leaf</li>
</ul>
<p>[min-max-nodes-balanced.png]</p>
<ul>
<li>Implementing as a balanced tree results in min number of parents and max number of leaves: 2 parents, 3
leaves</li>
</ul>
</li>
</ul>
</li>
<li>All that we need in order to implement a binary tree is a TreeNode class that can store a value and references to
a left and right child. We can create a tree by assigning the left and right properties to point to other TreeNode
instances:</li>
</ul>
<div id=">pre data-role=" codeBlock" data-info="js" class="language-javascript"><code><a title=" 1"> <span>class</span> TreeNode <span>{</span></a>
<a title="2"> <span>constructor</span>(val) <span>{</span></a>
<a title="3"> <span>this</span>.<span>val</span> <span>=</span> val<span>;</span></a>
<a title="4"> <span>this</span>.<span>left</span> <span>=</span> <span>null;</span></a>
<a title="5"> <span>this</span>.<span>right</span> <span>=</span> <span>null;</span></a>
<a id=-6" title="6"> <span>}</span></a>
<a title="7"> <span>}</span></a></code></pre>
</div>
<ol start="2" type="1">
<li>Identify the three types of tree traversals: pre-order, in-order, and post-order.</li>
</ol>
<ul>
<li>Pre-order: Values are accessed as soon as the node is reached.</li>
<li>In-order: Values are accessed after we have fully explored the left but before we explore the right branch.</li>
<li>Post-order: Values are accessed after all of our children have been accessed.</li>
<li>*Breadth First: The previous three are types of Depth First Traversals. Breadth first accesses values of nodes by
level, left to right, top to bottom.</li>
<li>
<p>Given a tree, be able to determine the order of each traversal type:</p>
<p>[Number tree]</p>
<ul>
<li>Breadth First: 20, 9, 24, 7, 11, 23, 27, 3, 10, 17, 36, 30</li>
<li>Pre-order: 20, 9, 7, 3, 11, 10, 17, 24, 23, 27, 36, 30</li>
<li>In-order: 3, 7, 9, 10, 11, 17, 20, 23, 24, 27, 30, 36</li>
<li>Post-order: 3, 7, 10, 17, 11, 9, 23, 30, 36, 27, 24, 20</li>
</ul>
</li>
</ul>
<ol start="3" type="1">
<li>Explain and implement a Binary Search Tree.</li>
</ol>
<ul>
<li>A binary search tree is a binary tree with the added stipulation that all values to the left of a node are less
than its value and all values to the right are greater than its value.
<ul>
<li>Example of a Binary Search Tree <img src="#binary-search-tree" alt="Binary Search Tree"/></li>
</ul>
</li>
<li>Example of a BST with an insert method. You won't be asked to implement a removal:</li>
</ul>
<div id=">pre data-role=" codeBlock" data-info="js" class="language-javascript"><code><a title=" 1"> <span>class</span> BST <span>{</span></a>
<a id=-2" title="2"> <span>constructor</span>() <span>{</span></a>
<a id=-3" title="3"> <span>this</span>.<span>root</span> <span>=</span> <span>null;</span></a>
<a id=-4" title="4"> <span>}</span></a>
<a id=-5" title="5"></a>
<a id=-6" title="6"> <span>insert</span>(val<span>,</span> currentNode <span>=</span>
<span>this</span>.<span>root</span>) <span>{</span></a>
<a id=-7" title="7"> <span>if</span> (<span>!this</span>.<span>root</span>) <span>{</span></a>
<a id=-8" title="8"> <span>this</span>.<span>root</span> <span>=</span> <span>new</span>
<span>TreeNode</span>(val)<span>;</span></a>
<a id=-9" title="9"> <span>return;</span></a>
<a id=-10" title="10"> <span>}</span></a>
<a id=-11" title="11"></a>
<a id=-12" title="12"> <span>if</span> (val <span><</span> <span>currentNode</span>.<span>val</span>)
<span>{</span></a>
<a id=-13" title="13"> <span>if</span> (<span>!currentNode</span>.<span>left</span>) <span>{</span></a>
<a id=-14" title="14"> <span>currentNode</span>.<span>left</span> <span>=</span> <span>new</span>
<span>TreeNode</span>(val)<span>;</span></a>
<a id=-15" title="15"> <span>}</span> <span>else</span> <span>{</span></a>
<a id=-16" title="16"> <span>this</span>.<span>insert</span>(val<span>,</span>
<span>currentNode</span>.<span>left</span>)<span>;</span></a>
<a id=-17" title="17"> <span>}</span></a>
<a id=-18" title="18"> <span>}</span> <span>else</span> <span>{</span></a>
<a id=-19" title="19"> <span>if</span> (<span>!currentNode</span>.<span>right</span>) <span>{</span></a>
<a id=-20" title="20"> <span>currentNode</span>.<span>right</span> <span>=</span> <span>new</span>
<span>TreeNode</span>(val)<span>;</span></a>
<a id=-21" title="21"> <span>}</span> <span>else</span> <span>{</span></a>
<a id=-22" title="22"> <span>this</span>.<span>insert</span>(val<span>,</span>
<span>currentNode</span>.<span>right</span>)<span>;</span></a>
<a id=-23" title="23"> <span>}</span></a>
<a id=-24" title="24"> <span>}</span></a>
<a id=-25" title="25"> <span>}</span></a>
<a id=-26" title="26"> <span>}</span></a></code></pre>
</div>
<h2 id="graphs">Graphs</h2>
<ol type="1">
<li>Know the differences between graphs and trees</li>
</ol>
<ul>
<li>A graph can:
<ul>
<li>Consist of any collection of nodes and edges (no limits on connections)</li>
<li>Have cycles</li>
<li>Have disconnected portions (a forest, with multiple trees, for example)</li>
<li>Be missing a root node (don't have to have one node that connects to everything)</li>
</ul>
</li>
<li>In a tree, we had an idea of children and parents, in a graph we have neighbors (no hierarchy)</li>
</ul>
<ol start="2" type="1">
<li>What are three ways that we can implement a graph? What are each implementations' advantages or disadvantages?
</li>
</ol>
<ul>
<li>Adjacency Matrix - 2D Array
<ul>
<li>Visually clear what's going on</li>
<li>One axis (outside array) has an entry (inner array) for each node in the graph. If one node is connected to
another node in the graph, our entry in the inner array is set to true. Otherwise the entry is false.</li>
</ul>
</li>
</ul>
<div id=">pre data-role=" codeBlock" data-info="js" class="language-javascript"><code><a title=" 1"> <span>let</span> matrix <span>=</span> [</a>
<a title="2"> <span>/* A B C D E F */</span></a>
<a title="3"> <span>/*A*/</span></a>
<a id=-4" title="4"> [<span>true,</span> <span>true,</span> <span>true,</span> <span>false,</span> <span>true,</span>
<span>false</span>]<span>,</span></a>
<a id=-5" title="5"> <span>/*B*/</span></a>
<a id=-6" title="6"> [<span>false,</span> <span>true,</span> <span>false,</span> <span>false,</span>
<span>false,</span> <span>false</span>]<span>,</span></a>
<a id=-7" title="7"> <span>/*C*/</span></a>
<a id=-8" title="8"> [<span>false,</span> <span>true,</span> <span>true,</span> <span>true,</span> <span>false,</span>
<span>false</span>]<span>,</span></a>
<a id=-9" title="9"> <span>/*D*/</span></a>
<a id=-10" title="10"> [<span>false,</span> <span>false,</span> <span>false,</span> <span>true,</span>
<span>false,</span> <span>false</span>]<span>,</span></a>
<a id=-11" title="11"> <span>/*E*/</span></a>
<a id=-12" title="12"> [<span>true,</span> <span>false,</span> <span>false,</span> <span>false,</span>
<span>true,</span> <span>false</span>]<span>,</span></a>
<a id=-13" title="13"> <span>/*F*/</span></a>
<a id=-14" title="14"> [<span>false,</span> <span>false,</span> <span>false,</span> <span>false,</span>
<span>true,</span> <span>true</span>]</a>
<a id=-15" title="15"> ]<span>;</span></a></code></pre>
</div>
<ul>
<li>Adjacency List - POJO
<ul>
<li>Object where every value in the graph has a key</li>
<li>Value for the key is an array with each other node that it is connected to (neighbors)</li>
<li>Easy to iterate through</li>
<li>Doesn't take up as much space as an Adjacency Matrix or Node</li>
<li>Can refer to the entire graph by referencing the object</li>
</ul>
</li>
</ul>
<div id=">pre data-role=" codeBlock" data-info="js" class="language-javascript"><code><a title=" 1"> <span>let</span> list <span>=</span> <span>{</span></a>
<a id=-2" title="2"> <span>a:</span> [<span>'b',</span> <span>'c',</span>
<span>'e'</span>]<span>,</span></a>
<a id=-3" title="3"> <span>b:</span> []<span>,</span></a>
<a id=-4" title="4"> <span>c:</span> [<span>'b',</span> <span>'d'</span>]<span>,</span></a>
<a id=-5" title="5"> <span>d:</span> []<span>,</span></a>
<a id=-6" title="6"> <span>e:</span> [<span>'a'</span>]<span>,</span></a>
<a id=7" title="7"> <span>f:</span> [<span>'e'</span>]</a>
<a id=-8" title="8"> <span>};</span></a></code></pre>
</div>
<ul>
<li>Object-Oriented (ex: using Nodes)
<ul>
<li>Similar to our linked list or tree implementations</li>
<li>Track the value and the neighbors array as instance variables on the node</li>
<li>We don't have a reference to the overall graph with this implementation</li>
</ul>
</li>
</ul>
<div>
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a title="1"> <span >class</span> GraphNode <span >{</span></a>
<a id=-2" title="2"> <span >constructor</span>(val) <span >{</span></a>
<a id=-3" title="3"> <span >this</span>.<span >val</span> <span >=</span> val<span >;</span></a>
<a id=-4" title="4"> <span >this</span>.<span >neighbors</span> <span >=</span> []<span >;</span></a>
<a id=-5" title="5"> <span >}</span></a>
<a id=-6" title="6"> <span >}</span></a></code></pre>
</div>
<ol start="3" type="1">
<li>Given a graph in one of the above implementations, be able to traverse the graph in a breadth-first or depth-first
manner.</li>
</ol>
<ul>
<li>We can use recursion or iteration to traverse each node.</li>
<li>We generally want to keep track of each node that we've visited already so that we don't get trapped in cycles.
Easiest way to do this is to keep a Set variable that we update as we traverse to each node.</li>
<li>The projects from W08D02 and their solutions are a great resource here.
<ul>
<li>Be comfortable with taking either an iterative or a recursive approach to traversing a graph, as well as being
able to work with either an adjacency list (like in the friendsOf problem) or a node class (like in the
breadthFirstSearch or maxValue problems).</li>
<li>Practice taking the implementation that you did in the project and converting it to a different
implementation. You probably used recursion for friendsOf, so try using iteration with a stack array, etc.</li>
</ul>
</li>
</ul>
<ol start="4" type="1">
<li>Be able to make conclusions from these traversals</li>
</ol>
<ul>
<li>Is it possible to get from node A to node B?
<ul>
<li>Here we're really implementing a search, like the breadthFirstSearch problem.</li>
</ul>
</li>
<li>What is the maximum/minimum value we can encounter if we start at node X?
<ul>
<li>Instead of returning a boolean, we want to compare values of nodes and return the appropriate value
<ul>
<li>If we do this recursively we can compare this node and to each of its neighbors values and return the
maximum up the call stack.</li>
<li>If we do this iteratively, we can keep a currentMax variable as we traverse and update it if we find a new
max value.</li>
</ul>
</li>
</ul>
</li>
<li>etc.</li>
</ul>
<h2 id="whiteboarding-tips-and-tricks"><strong>Whiteboarding Tips and Tricks</strong></h2>
<ul>
<li>Companies whiteboard because steps for solving wb problems are almost nearly identical to solving real-world
practical situations.
<ul>
<li>They are looking to test a few things:
<ul>
<li>Can you think algorithmically? Think efficiently?</li>
<li>Are you a good communicator?</li>
<li>Can you code neatly and correctly?</li>
</ul>
</li>
</ul>
</li>
</ul>
<p><strong><code>Correct Steps</code></strong></p>
<ol type="1">
<li>Repeat and clarify the problem.</li>
<li>Identify Edge Cases & I/O.</li>
<li>Pseudocode IF NECESSARY.</li>
<li>Implement Code.</li>
<li>Walk through an example input.</li>
<li>Det. Time and Space (Theta).
<ul>
<li>Time is expensive, space is cheap.</li>
<li>Ask interviewer is optimization is even necessary.</li>
</ul>
</li>
</ol>
<p><strong><code>Strategies</code></strong></p>
<ul>
<li>Hash Maps.</li>
<li>Divide and Conquer DP Programming.</li>
<li>Using Math Properties.</li>
<li><strong><code>Amortized Analysis</code></strong> : Method for analyzing a given algorithm's complexity, or how
much a resource, especially time or or memory, it takes to execute.</li>
<li>Keeping a Stack or Queue on the side to track values.</li>
<li>Keep two pointers for the same iteration.</li>
<li>Perform an operation twice.</li>
<li>Sort the input.</li>
<li>Approach the problem from the other end.</li>
<li>Use binary numbers instead of decimal numbers.</li>
<li>Use binary search instead of incrementation.</li>
</ul>
<p><strong>If you have seen the problem before, just tell them you have</strong>.</p>
<h1 id="notes"><strong>Notes</strong></h1>
<h2 id="graphs-1"><strong>Graphs</strong></h2>
<ul>
<li><strong>Graph</strong> : Any collection of nodes and edges, it is much more relaxed compared to other trees.
<ul>
<li>May lack a root node.</li>
<li>May have cycles.</li>
<li>May have any number of edges leaving a node.</li>
</ul>
</li>
</ul>
<figure>
<img
src="https://s3-us-west-1.amazonaws.com/appacademy-open-assets/data_structures_algorithms/graphs/images/graphs.png"
alt="graph"/>
<figcaption>graph</figcaption>
</figure>
<p><strong>GraphNode Class</strong></p>
<div id=">pre data-role=" codeBlock" data-info="js" class="language-javascript"><code><a title=" 1"><span>class</span> GraphNode <span>{</span></a>
<a id=-2" title="2"> <span>constructor</span>(val) <span>{</span></a>
<a id=-3" title="3"> <span>this</span>.<span>val</span> <span>=</span> val<span>;</span></a>
<a id=-4" title="4"> <span>this</span>.<span>neighbors</span> <span>=</span> []<span>;</span></a>
<a id=-5" title="5"> <span>}</span></a>
<a id=-6" title="6"><span>}</span></a>
<a id=-7" title="7"></a>
<a id=-8" title="8"><span>let</span> a <span>=</span> <span>new</span>
<span>GraphNode</span>(<span>"a"</span>)<span>;</span></a>
<a id=-9" title="9"><span>let</span> b <span>=</span> <span>new</span>
<span>GraphNode</span>(<span>"b"</span>)<span>;</span></a>
<a id=-10" title="10"><span>let</span> c <span>=</span> <span>new</span>
<span>GraphNode</span>(<span>"c"</span>)<span>;</span></a>
<a id=-11" title="11"><span>let</span> d <span>=</span> <span>new</span>
<span>GraphNode</span>(<span>"d"</span>)<span>;</span></a>
<a id=-12" title="12"><span>let</span> e <span>=</span> <span>new</span>
<span>GraphNode</span>(<span>"e"</span>)<span>;</span></a>
<a id=-13" title="13"><span>let</span> f <span>=</span> <span>new</span>
<span>GraphNode</span>(<span>"f"</span>)<span>;</span></a>
<a id=-14" title="14"><span>a</span>.<span>neighbors</span> <span>=</span> [b<span>,</span> c<span>,</span>
e]<span>;</span></a>
<a id=-15" title="15"><span>c</span>.<span>neighbors</span> <span>=</span> [b<span>,</span> d]<span>;</span></a>
<a id=-16" title="16"><span>e</span>.<span>neighbors</span> <span>=</span> [a]<span>;</span></a>
<a id=-17" title="17"><span>f</span>.<span>neighbors</span> <span>=</span> [e]<span>;</span></a></code></pre>
</div>
<ul>
<li><strong>Adjacency Matrix</strong> : Mathematician's preferred way of representing a graph.</li>
</ul>
<figure>
<img
src="https://s3-us-west-1.amazonaws.com/appacademy-open-assets/data_structures_algorithms/graphs/images/adj_matrix_graph.png"
alt="adja"/>
<figcaption>adja</figcaption>
</figure>
<div id="cb7">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a title="1"><span >let</span> matrix <span >=</span> [</a>
<a id="cb7-2" title="2"> <span >/* A B C D E F */</span></a>
<a id="cb7-3" title="3"> <span >/*A*/</span> [<span >true,</span> <span >true,</span> <span >true,</span> <span >false,</span> <span >true,</span> <span >false</span>]<span >,</span></a>
<a id="cb7-4" title="4"> <span >/*B*/</span> [<span >false,</span> <span >true,</span> <span >false,</span> <span >false,</span> <span >false,</span> <span >false</span>]<span >,</span></a>
<a id="cb7-5" title="5"> <span >/*C*/</span> [<span >false,</span> <span >true,</span> <span >true,</span> <span >true,</span> <span >false,</span> <span >false</span>]<span >,</span></a>
<a id="cb7-6" title="6"> <span >/*D*/</span> [<span >false,</span> <span >false,</span> <span >false,</span> <span >true,</span> <span >false,</span> <span >false</span>]<span >,</span></a>
<a id="cb7-7" title="7"> <span >/*E*/</span> [<span >true,</span> <span >false,</span> <span >false,</span> <span >false,</span> <span >true,</span> <span >false</span>]<span >,</span></a>
<a id="cb7-8" title="8"> <span >/*F*/</span> [<span >false,</span> <span >false,</span> <span >false,</span> <span >false,</span> <span >true,</span> <span >true</span>]<span >,</span></a>
<a id="cb7-9" title="9">]<span >;</span></a></code></pre>
</div>
<ul>
<li><strong>Adjacency List</strong> : Using an object to represent node labels.</li>
</ul>
<div id="cb8">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a title="1"><span >let</span> graph <span >=</span> <span >{</span></a>
<a id="cb8-2" title="2"> <span >a:</span> [<span >"b",</span> <span >"c",</span> <span >"e"</span>]<span >,</span></a>
<a id="cb8-3" title="3"> <span >b:</span> []<span >,</span></a>
<a id="cb8-4" title="4"> <span >c:</span> [<span >"b",</span> <span >"d"</span>]<span >,</span></a>
<a id="cb8-5" title="5"> <span >d:</span> []<span >,</span></a>
<a id="cb8-6" title="6"> <span >e:</span> [<span >"a"</span>]<span >,</span></a>
<a id="cb8-7" title="7"> <span >f:</span> [<span >"e"</span>]<span >,</span></a>
<a id="cb8-8" title="8"><span >};</span></a></code></pre>
</div>
<hr />
<h2 id="graph-traversal"><strong>Graph Traversal</strong></h2>
<p><strong>Traversal with Graph Node</strong> <strong>Depthfirst Recursion</strong></p>
<div id="cb9">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a title="1"><span >class</span> GraphNode <span >{</span></a>
<a id="cb9-2" title="2"> <span >constructor</span>(val) <span >{</span></a>
<a id="cb9-3" title="3"> <span >this</span>.<span >val</span> <span >=</span> val<span >;</span></a>
<a id="cb9-4" title="4"> <span >this</span>.<span >neighbors</span> <span >=</span> []<span >;</span></a>
<a id="cb9-5" title="5"> <span >}</span></a>
<a id="cb9-6" title="6"><span >}</span></a>
<a id="cb9-7" title="7"></a>
<a id="cb9-8" title="8"><span >let</span> a <span >=</span> <span >new</span> <span >GraphNode</span>(<span >"a"</span>)<span >;</span></a>
<a id="cb9-9" title="9"><span >let</span> b <span >=</span> <span >new</span> <span >GraphNode</span>(<span >"b"</span>)<span >;</span></a>
<a id="cb9-10" title="10"><span >let</span> c <span >=</span> <span >new</span> <span >GraphNode</span>(<span >"c"</span>)<span >;</span></a>
<a id="cb9-11" title="11"><span >let</span> d <span >=</span> <span >new</span> <span >GraphNode</span>(<span >"d"</span>)<span >;</span></a>
<a id="cb9-12" title="12"><span >let</span> e <span >=</span> <span >new</span> <span >GraphNode</span>(<span >"e"</span>)<span >;</span></a>
<a id="cb9-13" title="13"><span >let</span> f <span >=</span> <span >new</span> <span >GraphNode</span>(<span >"f"</span>)<span >;</span></a>
<a id="cb9-14" title="14"><span >a</span>.<span >neighbors</span> <span >=</span> [e<span >,</span> c<span >,</span> b]<span >;</span></a>
<a id="cb9-15" title="15"><span >c</span>.<span >neighbors</span> <span >=</span> [b<span >,</span> d]<span >;</span></a>
<a id="cb9-16" title="16"><span >e</span>.<span >neighbors</span> <span >=</span> [a]<span >;</span></a>
<a id="cb9-17" title="17"><span >f</span>.<span >neighbors</span> <span >=</span> [e]<span >;</span></a></code></pre>
</div>
<div id=0">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a id=0-1" title="1"><span >function</span> <span >depthFirstRecur</span>(node<span >,</span> visited <span >=</span> <span >new</span> <span >Set</span>()) <span >{</span></a>
<a id=0-2" title="2"> <span >// if this node has already been visited, then return early</span></a>
<a id=0-3" title="3"> <span >if</span> (<span >visited</span>.<span >has</span>(<span >node</span>.<span >val</span>)) <span >return;</span></a>
<a id=0-4" title="4"></a>
<a id=0-5" title="5"> <span >// otherwise it hasn't yet been visited,</span></a>
<a id=0-6" title="6"> <span >// so print it's val and mark it as visited.</span></a>
<a id=0-7" title="7"> <span >console</span>.<span >log</span>(<span >node</span>.<span >val</span>)<span >;</span></a>
<a id=0-8" title="8"> <span >visited</span>.<span >add</span>(<span >node</span>.<span >val</span>)<span >;</span></a>
<a id=0-9" title="9"></a>
<a id=0-10" title="10"> <span >// then explore each of its neighbors</span></a>
<a id=0-11" title="11"> <span >node</span>.<span >neighbors</span>.<span >forEach</span>((neighbor) <span >=></span> <span >{</span></a>
<a id=0-12" title="12"> <span >depthFirstRecur</span>(neighbor<span >,</span> visited)<span >;</span></a>
<a id=0-13" title="13"> <span >}</span>)<span >;</span></a>
<a id=0-14" title="14"><span >}</span></a></code></pre>
</div>
<div id=1">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a id=1-1" title="1"><span >function</span> <span >depthFirstIter</span>(node) <span >{</span></a>
<a id=1-2" title="2"> <span >let</span> visited <span >=</span> <span >new</span> <span >Set</span>()<span >;</span></a>
<a id=1-3" title="3"> <span >let</span> stack <span >=</span> [node]<span >;</span></a>
<a id=1-4" title="4"></a>
<a id=1-5" title="5"> <span >while</span> (<span >stack</span>.<span >length</span>) <span >{</span></a>
<a id=1-6" title="6"> <span >let</span> node <span >=</span> <span >stack</span>.<span >pop</span>()<span >;</span></a>
<a id=1-7" title="7"></a>
<a id=1-8" title="8"> <span >// if this node has already been visited, then skip this node</span></a>
<a id=1-9" title="9"> <span >if</span> (<span >visited</span>.<span >has</span>(<span >node</span>.<span >val</span>)) <span >continue;</span></a>
<a id=1-10" title="10"></a>
<a id=1-11" title="11"> <span >// otherwise it hasn't yet been visited,</span></a>
<a id=1-12" title="12"> <span >// so print it's val and mark it as visited.</span></a>
<a id=1-13" title="13"> <span >console</span>.<span >log</span>(<span >node</span>.<span >val</span>)<span >;</span></a>
<a id=1-14" title="14"> <span >visited</span>.<span >add</span>(<span >node</span>.<span >val</span>)<span >;</span></a>
<a id=1-15" title="15"></a>
<a id=1-16" title="16"> <span >// then add its neighbors to the stack to be explored</span></a>
<a id=1-17" title="17"> <span >stack</span>.<span >push</span>(...<span >node</span>.<span >neighbors</span>)<span >;</span></a>
<a id=1-18" title="18"> <span >}</span></a>
<a id=1-19" title="19"><span >}</span></a></code></pre>
</div>
<p><strong>Traversal with Adjacency List</strong></p>
<div id=2">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a id=2-1" title="1"><span >let</span> graph <span >=</span> <span >{</span></a>
<a id=2-2" title="2"> <span >a:</span> [<span >"b",</span> <span >"c",</span> <span >"e"</span>]<span >,</span></a>
<a id=2-3" title="3"> <span >b:</span> []<span >,</span></a>
<a id=2-4" title="4"> <span >c:</span> [<span >"b",</span> <span >"d"</span>]<span >,</span></a>
<a id=2-5" title="5"> <span >d:</span> []<span >,</span></a>
<a id=2-6" title="6"> <span >e:</span> [<span >"a"</span>]<span >,</span></a>
<a id=2-7" title="7"> <span >f:</span> [<span >"e"</span>]<span >,</span></a>
<a id=2-8" title="8"><span >};</span></a></code></pre>
</div>
<div id=3">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a id=3-1" title="1"><span >function</span> <span >depthFirst</span>(graph) <span >{</span></a>
<a id=3-2" title="2"> <span >let</span> visited <span >=</span> <span >new</span> <span >Set</span>()<span >;</span></a>
<a id=3-3" title="3"></a>
<a id=3-4" title="4"> <span >for</span> (<span >let</span> node <span >in</span> graph) <span >{</span></a>
<a id=3-5" title="5"> <span >_depthFirstRecur</span>(node<span >,</span> graph<span >,</span> visited)<span >;</span></a>
<a id=3-6" title="6"> <span >}</span></a>
<a id=3-7" title="7"><span >}</span></a>
<a id=3-8" title="8"></a>
<a id=3-9" title="9"><span >function</span> <span >_depthFirstRecur</span>(node<span >,</span> graph<span >,</span> visited) <span >{</span></a>
<a id=3-10" title="10"> <span >if</span> (<span >visited</span>.<span >has</span>(node)) <span >return;</span></a>
<a id=3-11" title="11"></a>
<a id=3-12" title="12"> <span >console</span>.<span >log</span>(node)<span >;</span></a>
<a id=3-13" title="13"> <span >visited</span>.<span >add</span>(node)<span >;</span></a>
<a id=3-14" title="14"></a>
<a id=3-15" title="15"> graph[node].<span >forEach</span>((neighbor) <span >=></span> <span >{</span></a>
<a id=3-16" title="16"> <span >_depthFirstRecur</span>(neighbor<span >,</span> graph<span >,</span> visited)<span >;</span></a>
<a id=3-17" title="17"> <span >}</span>)<span >;</span></a>
<a id=3-18" title="18"><span >}</span></a>
<a id=3-19" title="19"></a>
<a id=3-20" title="20"><span >depthFirst</span>(graph)<span >;</span></a></code></pre>
</div>
<h1 id="notes-1"><strong>Notes</strong></h1>
<h2 id="network-knowledge"><strong>Network Knowledge</strong></h2>
<h2 id="osi-network-model"><strong>OSI Network Model</strong></h2>
<ul>
<li>
<p><strong><code>OSI</code></strong> : The Open Systems Interconnection reference model is a well known Network
Model created in the UK that has both conceptual layers <strong>and</strong> suggested protocols for each.</p>
<ul>
<li>There are seven layers to the OSI Model. <img
src="https://assets.aaonline.io/Module-Web/network/image-network-models-osi.svg" alt="osilayer"/></li>
<li><strong><code>Application</code></strong> : Includes information used by client-side software, data from this
later interacts directly with applications. (i.e. HTTP)</li>
<li><strong><code>Presentation</code></strong> : The syntax later that converts data from machine-readable to
human-readable. Includes data compression, encryption, and character encoding. (i.e. JPEG & GIF)</li>
<li><strong><code>Session</code></strong> : Includes protocols responsible for authentication and data continuity.
Includes authorization or re-establishing a dropp connection. (i.e. RPC (Remote Procedure Call))</li>
<li><strong><code>Transport</code></strong> : Utilization of transport protocols (i.e. TCP and UDP)</li>
<li><strong><code>Network</code></strong> : Basically the internet layer (i.e. IP)</li>
<li><strong><code>Data Link</code></strong> : Deal with connections from one machine's network interface to
another. (i.e. ethernet)</li>
<li><strong><code>Physical</code></strong> : Translating raw electrical signals to bits & bytes of data.
(i.e. Wi-Fi & DSL)</li>
</ul>
</li>
<li>It is important to remember that OSI Model is highly conceptual, it's practical uses are limited.</li>
<li>
<p>TCP/IP is much more practical compared to OSI, but it is purely practical.</p>
</li>
</ul>
<hr />
<h2 id="tcpip-model"><strong>TCP/IP Model</strong></h2>
<ul>
<li><strong><code>Reference Model</code></strong> : A High-level overview of a complex topic provided by an
organization that manages it.</li>
</ul>
<p><strong>Layers of the TCP/IP Model</strong> <img
src="https://assets.aaonline.io/Module-Web/network/image-network-models-tcp-ip.svg" alt="tcpiplayer"/></p>
<ul>
<li><strong><code>Application</code></strong> : Includes protocols related to user-facing data. Anything that is
transmitted from the Transport layer is considered Application Layer Data (i.e. HTTP & FTP)</li>
<li><strong><code>Transport</code></strong> : TCP & UDP.</li>
<li><strong><code>Internet</code></strong> : Connects separate networks together (IP)</li>
<li><strong><code>Link</code></strong> : Lower-level communication standards.</li>
<li><strong><code>Physical?</code></strong> : There is a supposed fifth layer that cinludes all the electrical
concepts that span across wires, but it is not officially stated. <img
src="https://assets.aaonline.io/Module-Web/network/image-network-models-encapsulation.svg" alt="encap"/></li>
<li>We can think of the layers of our reference model as being <strong>encapsulated</strong>.</li>
</ul>
<hr />
<h2 id="binary-and-hexidecimal"><strong>Binary and Hexidecimal</strong></h2>
<ul>
<li><strong><code>Binary</code></strong> : Number expressed in the base-2 numeral system or binary numeral system.
<ul>
<li><strong><code>Base</code></strong> : Number System, computers use a Base 2 NS.</li>
</ul>
</li>
<li>
<p><strong><code>CPU</code></strong> : Central Processing Unit, an electronic circuitry within a computer that
executes instructions that make up a computer program.</p>
</li>
<li>It is easy to understand binary numbers if you remember the bases.</li>
<li>
<p>1, 2, 4, 8, 16, 32, 64, 128…etc.</p>
</li>
<li>
<p><strong><code>Bit</code></strong> : A single digit represented by either 1 (ON) or 2 (OFF). <img
src="https://i.gyazo.com/413dd89d209c0b2a4d10d8c1f6fe40b6.png" alt="pic"/></p>
</li>
<li>
<p><strong><code>Byte</code></strong> : Sequence of 8 Bits.</p>
</li>
<li><strong><code>Hexadecimal</code></strong> : Base 16, useful numeric system due to it making it easier for us to
read binary.
<ul>
<li>Helps to make up for base 10 only going from 0 - 9, adds in A, B, C, D, E, F.</li>
<li>You will sometimes see hexadecimals pre-pended with a <code>0x</code>.</li>
<li><code>FF = 255 = 11111111 = 1 byte</code></li>
</ul>
</li>
</ul>
pre data-role="codeBlock" data-info="js" class="language-javascript"><code>hexadecimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
decimal: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15</code></pre>
pre data-role="codeBlock" data-info="js" class="language-javascript"><code>Regular Numbers: 4 8 15 16 23 42
Binary: 00000100 00001000 00001111 00010000 00010111 00101010
Hexadecimal: 04 08 0F 10 17 2A</code></pre>
<ul>
<li>If you provide a numerical base in JS in the .toString() method you can convert things to binary and decimal.
(parseInt can also be used)</li>
</ul>
<div id=6">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a id=6-1" title="1"><span >Number</span>(<span >42</span>).<span >toString</span>(<span >16</span>)<span >;</span> <span >// 2a</span></a>
<a id=6-2" title="2"><span >Number</span>(<span >42</span>).<span >toString</span>(<span >2</span>)<span >;</span> <span >// 101010</span></a></code></pre>
</div>
<div id=7">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a id=7-1" title="1"><span >parseInt</span>(<span >"101010",</span> <span >2</span>)<span >;</span> <span >// 42</span></a>
<a id=7-2" title="2"><span >parseInt</span>(<span >"2A",</span> <span >16</span>)<span >;</span> <span >// 42</span></a></code></pre>
</div>
<hr />
<h2 id="internet-protocol"><strong>Internet Protocol</strong></h2>
<ul>
<li><strong><code>TCP</code></strong> : Transmission Control Protocol.
<ul>
<li>TCP is fault tolerant and end-to-end.</li>
<li>If data transmission fails it can be cached and re-sent.</li>
<li>No single central system that can take down the entire network.</li>
</ul>
</li>
<li><strong><code>IP</code></strong> : Internet Protocol.</li>
<li><strong><code>Internet</code></strong> : A series of interconnected networks sharing data.</li>
<li><strong><code>Packet</code></strong> : Format that IP data is packaged in.
<ul>
<li>Contains metadata in headers, and body content.</li>
</ul>
</li>
<li>
<p><strong><code>Pack-Switching</code></strong> : When a message is split up into separate ‘packets', delivered to a
destination, and reassembled as appropriate. > A Byte is 8 Bits > <img
src="https://assets.aaonline.io/Module-Web/network/image-ip-ipv4-headers.svg" alt="ip4"/></p>
</li>
<li>IPV4</li>
<li>
<p>IPv4 is still the most commonly used protocol version online.</p>
</li>
</ul>
<figure>
<img src="https://assets.aaonline.io/Module-Web/network/image-ip-ipv6-headers.svg" alt="ipv6"/>
<figcaption>ipv6</figcaption>
</figure>
<ul>
<li>IPV6</li>
<li>Was created because we are running out of IP Addresses.</li>
<li><strong><code>Version</code></strong> : Binary representation of the version #.</li>
<li><strong><code>Traffic Class</code></strong> : Used to Identify different types of packets.</li>
<li><strong><code>Flow Label</code></strong> : An experimental option used for adding packet sequencing into IP.</li>
<li><strong><code>Payload Length</code></strong> : Let's the receivers know how large the data in the packet will be.
</li>
<li><strong><code>Next Header</code></strong> : Usually identifies the protocol type of the packet's data.</li>
<li><strong><code>Hop Limit</code></strong> : Means of preventing packets from being passed around routers forever.
</li>
<li><strong><code>Source Address</code></strong> : Where the pack originated.</li>
<li><strong><code>Destination Address</code></strong> : Where the packet is heading. > All of the headers have a
fixed length of 40 bytes.</li>
</ul>
<p><strong>Special Addresses</strong></p>
<ul>
<li><strong>Local Host</strong> : Loopback Address, or the identifier for your current machine.
<ul>
<li><code>127.0.0.1</code></li>
</ul>
</li>
<li><strong>ALl-Interfaces Address</strong> : Used to catch any incoming requests regardless of the intended
destination.
<ul>
<li><code>0.0.0.0</code></li>
</ul>
</li>
</ul>
<hr />
<h2 id="transport-protocols"><strong>Transport Protocols</strong></h2>
<ul>
<li>Transport Protocols act as our delivery person.</li>
<li>IP is concerned with machine-to-machine communication.</li>
<li>HTTP is designed for application-to-application communication.</li>
<li><strong><code>Port</code></strong> : Virtual interfaces that allow a single device to host lot's of different
applications & services.</li>
<li>
<p><strong><code>TCP</code></strong> : Transmission Control Protocol, the most common transport protocol.</p>
<ul>
<li>Connection-oriented protocol.</li>
<li>Reliable protocol because the segments cannot be lost.</li>
<li>Heavy protocol to use (hence, sometimes there is buffering)</li>
</ul>
</li>
<li><strong><code>UDP</code></strong> : User Datagram Protocol.
<ul>
<li>Unreliable protocol.</li>
<li>Connection-less and provides no verification if data has been received.</li>
<li>Up to the sender/recipient to manage expectati</li>
</ul>
</li>
</ul>
<hr />
<h2 id="dns"><strong>DNS</strong></h2>
<ul>
<li><strong><code>DNS</code></strong> : Domain Name System is a distributed appraoch to providing easily-understood
names for internetworked devices. (Similar to a phonebook)
<ul>
<li>Invented as a way to distribute the work to numerous organizations, lightening the load and allowing much more
rapid growth.</li>
</ul>
</li>
<li><strong><code>Domain</code></strong> : A website's domain refers to the ‘friendly' name for the website's host, or
the server providing the site's content. <img
src="https://assets.aaonline.io/Module-Web/network/image-ip-dns-domain.svg" alt="url"/>
<ul>
<li>Top Level Domain the last part of the domain (.com, .net, .org)</li>
<li>Second Level Domain (Name of the website)</li>
<li>Subdomain (www)</li>
</ul>
</li>
<li><strong><code>Resolution</code></strong> : Process of working out which name server is needed.
<ul>
<li>During resolving, we work from right to left.</li>
</ul>
</li>
</ul>
<p><strong>DNS Records</strong></p>
<ul>
<li><strong><code>Zone File</code></strong> : Text file containing, host names, IP Addresses, and resource types.</li>
<li>Common DNS Record Types:
<ul>
<li><strong><code>SOA</code></strong> : Start of Authority, let's use know what name server is the primary
authority (<strong><code>THE MINIMUM REQUIREMENT IN A ZONE FILE</code></strong>)</li>
<li><strong><code>NS</code></strong> : Keeps us connected to our zone by pointing to name servers.</li>
<li><strong><code>A/AAAA</code></strong> : A = Domain Name to IPv4 & AAAA = Domain Name to IPv6</li>
<li><strong><code>CNAME</code></strong> : Links Domain name to Domain Name.</li>
<li><strong><code>MX</code></strong> : Mail Exchanger, used by e-mail clients.</li>
<li><strong><code>TTL</code></strong> : Time to live, measure of how long a record should be cached by a DNS name
server.</li>
</ul>
</li>
</ul>
<hr />
<h2 id="network-hardware"><strong>Network Hardware</strong></h2>
<ul>
<li>
<p><strong>HUB</strong> : Simplest networking device aka a Signal Splitter.</p>
<ul>
<li>Cheap and found in older networks.</li>
<li>Hubs do not do any filtering, every single data packet is sent to every single device all the time.</li>
<li>No one uses them anymore.</li>
</ul>
</li>
<li><strong>Switches: Traffic Control</strong> : Intelligent Hubs, track devices connected to them, help manage
network load, and can manage separate internal networks with ease.
<ul>
<li>The Max Address Table is the largest difference from hubs.</li>
<li><strong><code>Flood</code></strong> : If a destination address is unknown, the switch will flood received data
out to all connect devices.</li>
<li><strong><code>Forward</code></strong> : If the destination is known, it will send data directly to that
device.</li>
<li><strong><code>Filter</code></strong> : When data is dropped entirely from a transfer.</li>
</ul>
</li>
<li><strong><code>Routers</code></strong> : Connect separate networks with each other.
<ul>
<li>Participate in a process called NAT (Network Address Translation)</li>
</ul>
</li>
</ul>
<hr />
<h2 id="big-o-notation">Big O Notation</h2>
<h3 id="time-complexity">time complexity</h3>
<p>it allow us to talk formally about how the runtime of an algorithm grows as the input grows.</p>
<p>n = number of operation the computer has to do can be: f(n) = n f(n) = n^2 f(n) = 1</p>
<p>f(n) = could be something entirely different !</p>
<p><img src="./assets/ece920b.png"/></p>
<p>O(n):</p>
<div id=8">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a id=8-1" title="1"><span >function</span> <span >addUpToSimple</span>(n<span >:</span> <span >number</span>) <span >{</span></a>
<a id=8-2" title="2"> <span >let</span> total <span >=</span> <span >0;</span></a>
<a id=8-3" title="3"> <span >for</span> (<span >let</span> i <span >=</span> <span >0;</span> i <span ><</span> n<span >;</span> i<span >++</span>) <span >{</span></a>
<a id=8-4" title="4"> total <span >+=</span> i<span >;</span></a>
<a id=8-5" title="5"> <span >}</span></a>
<a id=8-6" title="6"> <span >return</span> total<span >;</span></a>
<a id=8-7" title="7"><span >}</span></a></code></pre>
</div>
<p>O(1):</p>
<div id=9">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a id=9-1" title="1"><span >function</span> <span >addUpComplex</span>(n<span >:</span> <span >number</span>) <span >{</span></a>
<a id=9-2" title="2"> <span >return</span> (n <span >*</span> (n <span >+</span> <span >1</span>)) / <span >2;</span></a>
<a id=9-3" title="3"><span >}</span></a></code></pre>
</div>
<p>O(n): maybe thinking O(2n) but we see big picture! BigONotation doesn't care about precision only about general
trends <em>linear? quadric? constant?</em></p>
<div id=0">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a id=0-1" title="1"><span >function</span> <span >printUpAndDown</span>(n<span >:</span> <span >number</span>) <span >{</span></a>
<a id=0-2" title="2"> <span >console.</span><span >log</span>(<span >"Going up"</span>)<span >;</span></a>
<a id=0-3" title="3"> <span >for</span> (<span >let</span> i <span >=</span> <span >0;</span> i <span ><</span> n<span >;</span> i<span >++</span>) <span >{</span></a>
<a id=0-4" title="4"> <span >console.</span><span >log</span>(i)<span >;</span></a>
<a id=0-5" title="5"> <span >}</span></a>
<a id=0-6" title="6"> <span >console.</span><span >log</span>(<span >"Going down"</span>)<span >;</span></a>
<a id=0-7" title="7"> <span >for</span> (<span >let</span> j <span >=</span> n <span >-</span> <span >1;</span> j <span >></span> <span >0;</span> j<span >--</span>) <span >{</span></a>
<a id=0-8" title="8"> <span >console.</span><span >log</span>(j)<span >;</span></a>
<a id=0-9" title="9"> <span >}</span></a>
<a id=0-10" title="10"><span >}</span></a></code></pre>
</div>
<p>O(n^2)</p>
<div id=1">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a id=1-1" title="1"><span >function</span> <span >printAllPairs</span>(n<span >:</span> <span >number</span>) <span >{</span></a>
<a id=1-2" title="2"> <span >for</span> (<span >let</span> i <span >=</span> <span >0;</span> i <span ><</span> n<span >;</span> i<span >++</span>) <span >{</span></a>
<a id=1-3" title="3"> <span >console.</span><span >log</span>(i)<span >;</span></a>
<a id=1-4" title="4"> <span >for</span> (<span >let</span> j <span >=</span> <span >0;</span> j <span ><</span> n<span >;</span> j<span >++</span>) <span >{</span></a>
<a id=1-5" title="5"> <span >console.</span><span >log</span>(j)<span >;</span></a>
<a id=1-6" title="6"> <span >}</span></a>
<a id=1-7" title="7"> <span >}</span></a>
<a id=1-8" title="8"><span >}</span></a></code></pre>
</div>
<p>O(n) : cuz as soon as n grows complexity grows too</p>
<div id=2">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a id=2-1" title="1"><span >function</span> <span >logAtLeastFive</span>(n<span >:</span> <span >number</span>) <span >{</span></a>
<a id=2-2" title="2"> <span >for</span> (<span >let</span> i <span >=</span> <span >0;</span> i <span ><=</span> <span >Math.</span><span >max</span>(<span >5,</span> n)<span >;</span> i<span >++</span>) <span >{</span></a>
<a id=2-3" title="3"> <span >console.</span><span >log</span>(i)<span >;</span></a>
<a id=2-4" title="4"> <span >}</span></a>
<a id=2-5" title="5"><span >}</span></a></code></pre>
</div>
<p>O(1)</p>
<div id=3">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a id=3-1" title="1"><span >function</span> <span >logAtMostFive</span>(n<span >:</span> <span >number</span>) <span >{</span></a>
<a id=3-2" title="2"> <span >for</span> (<span >let</span> i <span >=</span> <span >0;</span> i <span ><=</span> <span >Math.</span><span >min</span>(<span >5,</span> n)<span >;</span> i<span >++</span>) <span >{</span></a>
<a id=3-3" title="3"> <span >console.</span><span >log</span>(i)<span >;</span></a>
<a id=3-4" title="4"> <span >}</span></a>
<a id=3-5" title="5"><span >}</span></a></code></pre>
</div>
<h3 id="space-complexity">space complexity</h3>
<p>Rules of Thumb</p>
<ul>
<li>most primitive <em>booleans numbers undefined null</em> are constant space.</li>
<li>strings and reference types like objects an arrays require O(n) space <em>n is string length or number of
keys</em></li>
</ul>
<p>O(1)</p>
<div id=4">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a id=4-1" title="1"><span >function</span> <span >sum</span>(arr<span >:</span> <span >number[]</span>) <span >{</span></a>
<a id=4-2" title="2"> <span >let</span> total <span >=</span> <span >0;</span></a>
<a id=4-3" title="3"> <span >for</span> (<span >let</span> i <span >=</span> <span >0;</span> i <span ><</span> <span >arr.</span><span >length;</span> i<span >++</span>) <span >{</span></a>
<a id=4-4" title="4"> total <span >+=</span> arr<span >[</span>i<span >];</span></a>
<a id=4-5" title="5"> <span >}</span></a>
<a id=4-6" title="6"><span >}</span></a></code></pre>
</div>
<p>O(n)</p>
<div id=5">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a id=5-1" title="1"><span >function</span> <span >double</span>(arr<span >:</span> <span >number[]</span>) <span >{</span></a>
<a id=5-2" title="2"> <span >const</span> newArr <span >=</span> <span >[];</span></a>
<a id=5-3" title="3"> <span >for</span> (<span >let</span> i <span >=</span> <span >0;</span> i <span ><</span> <span >arr.</span><span >length;</span> i<span >++</span>) <span >{</span></a>
<a id=5-4" title="4"> <span >array.</span><span >push</span>(arr<span >[</span>i<span >]</span> <span >*</span> <span >2</span>)<span >;</span></a>
<a id=5-5" title="5"> <span >}</span></a>
<a id=5-6" title="6"> <span >return</span> newArr<span >;</span></a>
<a id=5-7" title="7"><span >}</span></a></code></pre>
</div>
<h3 id="quick-note-around-object-array-through-bigo-lens">quick note around object, array through BigO lens!</h3>
<p>object:</p>
<div id=6">
pre data-role="codeBlock" data-info="js" class="language-javascript"><code><a id=6-1" title="1"><span >const</span> person <span >=</span> <span >{</span> name<span >:</span> <span >"John",</span> age<span >:</span> <span >22,</span> hobbies<span >:</span> <span >["reading",</span> <span >"sleeping"]</span> <span >};</span></a>
<a id=6-2" title="2"></a>
<a id=6-3" title="3"><span >Object</span>.<span >keys</span>(person)<span >;</span> <span >// ["name", "age", "hobbies"] O(n)</span></a>
<a id=6-4" title="4"><span >Object</span>.<span >values</span>(person)<span >;</span> <span >// ["John", 22, Array(2)] O(n)</span></a>
<a id=6-5" title="5"><span >Object</span>.<span >entries</span>(person)<span >;</span> <span >// [Array(2), Array(2), Array(2)] O(n)</span></a>
<a id=6-6" title="6"><span >person.</span><span >hasOwnProperty</span>(<span >"name"</span>)<span >;</span> <span >// true O(1)</span></a></code></pre>
</div>
<p>array: <em>push() and pop()</em> are always faster from <em>unshift() and shift()</em> cuz inserting or removing
element from beginning of an array needs reIndexing all elements</p>
<h2 id="common-patterns">Common Patterns</h2>
<h3 id="frequency-counter">frequency counter</h3>
<p>O(n^3)</p>
<div id=7">
pre data-role=