CS Theory Flashcards

1
Q

What is recursion and give an example using javascript?

A

“Reading:
https://medium.com/backticks-tildes/iteration-vs-recursion-c2017a483890
Recursion: when a function calls itself

The concept of Recursion and Iteration is to execute a set of instructions repeatedly. The difference between them is that recursion is simply a method call in which the method being called is the same as the one making the call while iteration is when a loop is repeatedly executed until a certain condition is met. Under the hood, both recursion and iteration depends on a condition so as to know when to stop but recursion is simply a process, always applied to a function.
An example of recursion is shown below:

Example:
function factorial(x) {
 if (x < 0) return;
 if (x === 0) return 1;
 return x * factorial(x - 1);
}
factorial(3);
// 6

and here’s an example of iteration:

Key Differences between Recursion and Iteration
A conditional statement decides the termination of recursion while a control variable’s value decide the termination of the iteration statement (except in the case of a while loop).
Infinite recursion can lead to system crash whereas, infinite iteration consumes CPU cycles.
Recursion repeatedly invokes the mechanism, and consequently the overhead, of method calls. This can be expensive in both processor time and memory space while iteration doesn’t.
Recursion makes code smaller while iteration makes it longer.
These are some of the key differences between iteration and recursion. I hope this has been explanatory.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are types?

A

“https://github.com/getify/You-Dont-Know-JS/blob/master/types%20%26%20grammar/ch1.md
JavaScript has seven built-in types: null, undefined, boolean, number, string, object, symbol. They can be identified by the typeof operator.

Variables don’t have types, but the values in them do. These types define intrinsic behavior of the values.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are data structures?

A
"Data structures are the way we are able to store and retrieve data. 
Examples: arrays and objects.
Data structures handle four main functions for us:
Inputting information
Processing information
Maintaining information
Retrieving information
"

OO or class oriented programming stresses that data intrinsically has associated behavior (of course, different depending on the type and nature of the data!) that operates on it, so proper design is to package up (aka, encapsulate) the data and the behavior together. This is sometimes called “data structures” in formal computer science.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is an algorithm?

A

An algorithm is a step by step list of instructions telling the computer exactly what you want it to do. Within these instructions, the programmer tells the computer how to execute those actions.

“An algorithm is a self-contained series of instructions to perform a function.

In other words, an algorithm is a means of describing a way to solve a problem so that it can be solved repeatedly, by humans or machines. Computer scientists compare the efficiency of algorithms through the concept of ““Algorithmic Complexity”” or ““Big O”” notation.

For example:

A cooking recipe is a simple algorithm for humans
A sorting algorithm is often used in computer programming to explain a machine how to sort data
Common Algorithms are Pathfinding Algorithms such as the Traveling Salemen Problem, Tree Traversal Algorithms, etc..

There are also Machine Learning Algorithms such as Linear Regression, Logistic Regression, Decision Tree, Random Forest, Support Vector Machine, Recurrent Neural Network (RNN), Long Short Term Memory (LSTM) Neural Network, Convolutional Neural Network (CNN), Deep Convolutional Neural Network etc.”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is scope / lexical scope in javascript?

A

“Scope determines where variables, functions, and objects are accessible in your code during runtime. This means the scope of a variable is controlled by the location of the variable declaration - the two locations are global and local.
Lexical Scope defines how variable names are resolved in nested functions: inner functions contain the scope of parent functions even if the parent function has returned.”

“A relatively basic concept in JavaScript is that each declared function creates its own scope. Lexical scope is the scope model used by the JavaScript language, which differs to some other languages which use dynamic scope. Lexical scope is the scope defined at lexing time.

This digs into the mechanics of how JavaScript engine works. Despite commonly being referred to as an interpreted language, JavaScript compiles code immediately before executing it. For example the statement:var a = 2;is split into two separate steps at lexing time:
•        var aThis declares the variable in the scope, before code execution.
•        a = 2This assigns the value 2 to the variable a, if it is found in the available scope.

http://astronautweb.co/javascript-lexical-scope/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is polymorphism?

A

Polymorphism is the ability to write a function that handles many data-types — it is a fundamental form of abstraction and happens through inheritance in Object-Oriented Programming. Polymorphism allows the ability to create a variable, a function, or an object that takes on more than one form.

“In programming languages and type theory, polymorphism is the provision of a single interface to entities of different types[1] or the use of a single symbol to represent multiple different types.

The most commonly recognised major classes of polymorphism are:
• Ad hoc polymorphism: defines a common interface for an arbitrary set of individually specified types.
• Parametric polymorphism: when one or more types are not specified by name but by abstract symbols that can represent any type.
• Subtyping(also calledsubtype polymorphismorinclusion polymorphism): when a name denotes instances of many different classes related by some common superclass.[3]

https://en.wikipedia.org/wiki/Polymorphism_(computer_science)

The fancy word for the ability of multiple object types to implement the same functionality is polymorphism.

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Object-oriented_JS”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is encapsulation?

A

“Is information hiding.
It’s about hiding as much as possible of the object’s internal parts and exposing aminimal information about an object to the public.
The easiest way to create encapsulation in JavaScript is by using closures. A closure in this instance can be created as a function with private state

“Inobject oriented programming languages,encapsulationis used to refer to one of two related but distinct notions, and sometimes to the combination thereof:
• A language mechanism for restricting direct access to some of theobject’s components.
• A language construct that facilitates the bundling of data with themethods(or other functions) operating on that data.
Some programming language researchers and academics use the first meaning alone or in combination with the second as a distinguishing feature ofobject-oriented programming, while some programming languages that providelexical closuresview encapsulation as a feature of the languageorthogonalto object orientation.
The second definition is motivated by the fact that in many OOP languages, components are not hidden automatically and this can be overridden; thus,information hidingis defined as a separate notion by those who prefer the second definition.
The features of encapsulation are supported using classes in most object-oriented programming languages, although other alternatives also exist.

https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a Linked List

A

“stores data with nodes that point to other nodes.
Nodes, at its most basic it has one datum and one reference (another node).
A linked list chains nodes together by pointing one node’s reference towards another node.”

“A Linked List, that as its name says, is a linked list of nodes that are represents by a head that is the first node in the list and the tail that is the last one. Each node has a reference/pointer to the previous and the next node.

The linked list data structure have two types, the first one is single linked list, the nodes of this type have a pointer to the next one but not for their previous node.

https://hackernoon.com/the-little-guide-of-linked-list-in-javascript-9daf89b63b54”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a Doubly Linked List?

A

“A doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes.

https://medium.com/dev-blogs/ds-with-js-linked-lists-ii-3b387596e27e

In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains three fields: two link fields (references to the previous and to the next node in the sequence of nodes) and one data field. The beginning and ending nodes’ previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked lists formed from the same data items, but in opposite sequential orders.

https://en.wikipedia.org/wiki/Doubly_linked_list”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a Queue?

A

“FIFO (First In First Out)
Similar to a stack, queue is a linear data structure where it obeys the FIFO (First In First Out) mechanism. You can think of a queue as a single line of people at a fast food restaurants. The basic methods for a stack like push, pop, and peek.

https://medium.com/javascript-in-plain-english/javascript-what-are-stack-and-queue-79df7af5a566”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is a Stack?

A

“stack of pancakes
LIFO (Last In First Out)

What is a Stack?
A stack is a type of data structure that is linear where order is conserved. For many people, stack is known to have a LIFO (Last In First Out) mechanism. Let’s take a look at an example of a stack in real life: stack of plates.

https://medium.com/javascript-in-plain-english/javascript-what-are-stack-and-queue-79df7af5a566”

“A stack is a data structure + a container of objects that are added and removed according to the last-in first-out (LIFO) principle - elements can be added and removed from the stack only at the top.

A helpful analogy is to think of a stack of books; you can remove only the top book, also you can add a new book on the top.
A stack is a recursive data structure.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is a Hash Table

A

Hashtable is a data structure that associates keys with values;

“Hashing is a technique that is used to uniquely identify a specific object from a group of similar objects.

https://www.hackerearth.com/practice/data-structures/hash-tables/basics-of-hash-tables/tutorial/

In computing, a hash table (hash map) is a data structure that implements an associative array abstract data type, a structure that can map keys to values. A hash table uses a hash function to compute an index into an array of buckets or slots, from which the desired value can be found.

Ideally, the hash function will assign each key to a unique bucket, but most hash table designs employ an imperfect hash function, which might cause hash collisions where the hash function generates the same index for more than one key. Such collisions must be accommodated in some way.

https://en.wikipedia.org/wiki/Hash_table

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is a Heap?

A

” A heap is a tree-based data structure which satisfies the heap property:
Max heap: if the parent node is greater than the child node is called
Min Heap: if the parent node is less than the child node is called
The common heap is: binary heap, which is a binary tree”

“A Heap is a runtime concept in JavaScript. Objects are allocated in a heap which is just a name to denote a large mostly unstructured region of memory.

There are three runtime concepts in JS named: stack, heap, and queu. JavaScript has a concurrency model based on an ““event loop””. [Diagram to the right]

https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop#Heap

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is a Trie?

A

“It is a kind of search tree—an ordered tree data structure that is used to store a dynamic set or associative array where the keys are usually strings.
Unlike the binary search tree, no node in the tree stores the key associated with that node; instead, its position in the tree defines the key with which it is associated.

“Tries is a tree that stores strings. Maximum number of children of a node is equal to size of alphabet. Trie supports search, insert and delete operations in O(L) time where L is length of key.

https: //www.geeksforgeeks.org/advantages-trie-data-structure/
https: //www.geeksforgeeks.org/trie-insert-and-search/”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a Tree?

A

“a tree is a data structure that simulates hierarchical data with nodes. Each node of a tree holds its own data and pointers to other nodes.
Example: creating an HTML document and interacting with the DOM is an example of a working with a tree

“A tree is an important data structure of computer science which is useful for storing hierarchically ordered data. Unlike stack, queue and linear lists (arrays and linked lists) which are used to store linear data like marks of students in a class, list of tasks to be done, etc, trees are used to store non-linear data structures in a hierarchical order. A family tree is the most common example of hierarchical data. Directory structure, corporate structure, etc are also common examples of hierarchical data. The pictures given below show examples of a linear data structure as well as trees.

https://www.codesdope.com/blog/article/trees-in-computer-science/ “

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is a Binary Search Tree?

A

“Binary Search Tree is a node-based binary tree data structure which has the following properties:
The left subtree of a node contains only nodes with keys lesser than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
The left and right subtree each must also be a binary search tree.

“Binary Search Tree is a node-based binary tree data structure which has the following properties:

The left subtree of a node contains only nodes with keys lesser than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
The left and right subtree each must also be a binary search tree.
https://www.geeksforgeeks.org/binary-search-tree-data-structure/

In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.[2] Please do not get confused between a binary tree and a binary search tree.

The difference between a binary tree and a binary search tree is binary trees are not ordered whilst a binary search tree is ordered.
https://computersciencewiki.org/index.php/Binary_tree

17
Q

What is a Disjoint Set?

A

Disjoint-set data structure is a data structure that tracks a set of elements partitioned into a number of non-overlapping subsets.

A Disjoint set is a data structure in computer science (also called a union–find data structure or merge–find set) is a data structure that tracks a set of elements partitioned into a number of disjoint (non-overlapping) subsets. It provides near-constant-time operations (bounded by the inverse Ackermann function) to add new sets, to merge existing sets, and to determine whether elements are in the same set. In addition to many other uses disjoint-sets play a key role in Kruskal’s algorithm for finding the minimum spanning tree of a graph.

18
Q

What is a Bloom Filter?

A

A Bloom filter is a data structure designed to tell you, quickly and memory-efficiently, whether an element is present in a set.

“A Bloom filter is a space-efficient probabilistic data structure, conceived by Burton Howard Bloom in 1970, that is used to test whether an element is a member of a set. False positive matches are possible, but false negatives are not – in other words, a query returns either ““possibly in set”” or ““definitely not in set””. Elements can be added to the set, but not removed (though this can be addressed with a ““counting”” filter); the more elements that are added to the set, the larger the probability of false positives.

Bloom proposed the technique for applications where the amount of source data would require an impractically large amount of memory if ““conventional”” error-free hashing techniques were applied. He gave the example of a hyphenation algorithm for a dictionary of 500,000 words, out of which 90% follow simple hyphenation rules, but the remaining 10% require expensive disk accesses to retrieve specific hyphenation patterns. With sufficient core memory, an error-free hash could be used to eliminate all unnecessary disk accesses; on the other hand, with limited core memory, Bloom’s technique uses a smaller hash area but still eliminates most unnecessary accesses. For example, a hash area only 15% of the size needed by an ideal error-free hash still eliminates 85% of the disk accesses.

More generally, fewer than 10 bits per element are required for a 1% false positive probability, independent of the size or number of elements in the set.

http://pkirs.utep.edu/cis3355/Tutorials/chapter9/tutorial9A/bubblesort.htm”

19
Q

Demonstrate Bubble Sort and explain when you might use it?

A

“It’s a sorting algorithm, commonly used in computer science
Bubble Sort is based on the idea of repeatedly comparing pairs of adjacent elements and then swapping their positions if they’re in the wrong order.
When you might use it?

“A bubble sort is an internal exchange sort. It is considered one of the simplest methods to sort an array of objects. It is also known as a sinking sort (because the smallest items ““sink”” to the bottom of the array).

Instead of searching an array as a whole, the bubble sort works by comparing adjacent pairs of objects in the array. If the objects are not in the correct ordered, they are swapped so that the largest of the two moves up. This process continues until the largest of the objects, eventually ““bubbles”” up to the highest position in the array. After this occurs, the search for the next largest object begins. The swapping continues until the whole array is in the correct order.

For example:

We have ten elements in an array. That means we need to make 9 comparisons so that the largest element will bubble to the top of the array. Why 9 comparisons?

http://pkirs.utep.edu/cis3355/Tutorials/chapter9/tutorial9A/bubblesort.htm

20
Q

Demonstrate Insertion Sort and explain when you might use it?

A

“Insertion sort is a comparison sort used for sorting an array (or list).
A comparison sort is a sorting algorithm that compares the current values with other values within the same array or list that is being sorted.

When might you use it?

“Insertion sortis a simplesorting algorithmthat builds the finalsorted array(or list) one item at a time. It is much less efficient on large lists than more advanced algorithms such asquicksort,heapsort, ormerge sort. However, insertion sort provides several advantages:
• Simple implementation:Jon Bentleyshows a three-lineCversion, and a five-lineoptimizedversion[2]
• Efficient for (quite) small data sets, much like other quadratic sorting algorithms
• More efficient in practice than most other simple quadratic (i.e.,O(n2)) algorithms such asselection sortorbubble sort
• Adaptive, i.e., efficient for data sets that are already substantially sorted: thetime complexityisO(kn) when each element in the input is no more thankplaces away from its sorted position
• Stable; i.e., does not change the relative order of elements with equal keys
• In-place; i.e., only requires a constant amount O(1) of additional memory space
• Online; i.e., can sort a list as it receives it
When people manually sort cards in a bridge hand, most use a method that is similar to insertion sort.

https://en.wikipedia.org/wiki/Insertion_sort

21
Q

Demonstrate Merge Sort and explain when you might use it?

A

“What merge sort does is it splits the unsorted array into two parts and then you recursively apply merge sort to these sub-arrays to further split the arrays until you are left with a bunch of single-element arrays.

When you might use it?

“In computer science, merge sort (also commonly spelled mergesort) is an efficient, general-purpose, comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the order of equal elements is the same in the input and output. Merge sort is a divide and conquer algorithm that was invented by John von Neumann in 1945.[2] A detailed description and analysis of bottom-up mergesort appeared in a report by Goldstine and von Neumann as early as 1948.

An example of merge sort. First divide the list into the smallest unit (1 element), then compare each element with the adjacent list to sort and merge the two adjacent lists. Finally all the elements are sorted and merged.

https://en.wikipedia.org/wiki/Merge_sort”

22
Q

Demonstrate Quicksort and explain when you might use it?

A

“Quicksort works by picking an element from the array and labeling it as the “pivot.” All the rest of the elements in the array are split into two categories — they are either less than or greater than this pivot element.

When might you use it?

“Quicksort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of a random access file or an array in order. Developed by British computer scientist Tony Hoare in 1959[1] and published in 1961,[2] it is still a commonly used algorithm for sorting. When implemented well, it can be about two or three times faster than its main competitors, merge sort and heapsort.[3][contradictory]

Quicksort is a comparison sort, meaning that it can sort items of any type for which a ““less-than”” relation (formally, a total order) is defined. In efficient implementations it is not a stable sort, meaning that the relative order of equal sort items is not preserved. Quicksort can operate in-place on an array, requiring small additional amounts of memory to perform the sorting. It is very similar to selection sort, except that it does not always choose worst-case partition.

Mathematical analysis of quicksort shows that, on average, the algorithm takes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare.

https://en.wikipedia.org/wiki/Quicksort”