CS Theory Flashcards

(11 cards)

1
Q

What is recursion and give an example using javascript?

A

Explanation: Recursion is when a function calls itself within its own body. Besides the recursive call, it should always have a base case which stops it from calling itself to prevent infinite loops.

Use: Recursion is made for solving problems that can be broken down into smaller, repetitive problems. It is especially good for working on things that have many possible branches and are too complex for an iterative approach.

Example: A classic example is computing a factorial given a number num:
function factorial(num) {
if (num === 1) {
return num;
}
return num * factorial(num - 1);
}

Source: https://developer.mozilla.org/en-US/docs/Glossary/Recursion

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

What are types?

A

Explanation: Types, also called data types, each have a unique set of rules/instructions of what can and can’t be done with it.

Use: Types are necessary so that the computer knows how to handle data when trying to do an operation with it.

Example: A few data types that are shared by most programming language are:
Boolean (ex. true or false)
String (“hello world”)
Float (3.1415)

Source: https://www.youtube.com/watch?v=A37-3lflh8I

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

What are data structures?

A

Explanation: Data structures is storage that is used to store and organize data. It is also a way of arranging data on a computer in such a way that it can be updated and accessed efficiently.

Use: Data structures are not only used for processing, retrieving, and storing data, but also organizing the data into more readable ways.

Example: There are many types of data structures, all classified as either linear or non-linear. The following are some examples:
Linear Static Data Structures (Arrays)
Linear Dynamic Data Structures (Queue, Stack, Linked List)
Non-linear Data Structures (Tree and Graphs)

Source: https://www.geeksforgeeks.org/data-structures/

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

What is an algorithm?

A

Explanation: In literal terms, an algorithm is a set of rules to be followed in calculations or other problem-solving opterations or a procedure for solving a mathematical problem in a finite number of steps that frequently by recursive operations. In other words, an Algorithm refers to a sequence of finite steps to solve a particular problem.

Use: Algorithms can be used for many things such as building a solution by searching all available solutions, searching, and sorting.

Example:
Breadth First Search Algorithm
Recursive Algorithms
Brute Force Algorithms

Source: https://www.geeksforgeeks.org/introduction-to-algorithms/

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

Explanation: Scope refers that the accessibility of variables, depending on where they are declared in the code they are “visible” and thus can be called. Lexically scoped variables can only be called from within the block of code they are defined, generally speaking inside of a function.

Use: It is used to avoid scope pollution, or unwanted invoking of variables

Source:

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

What is polymorphism?

A

Explanation: Polymorphism is a concept of Object-oriented programming(OOP) Paradigm that provides a way to perform a single action in different ways.

Use: It provides an ability to call the same method on different JavaScript objects

Example:
class A {
display() {
console.log(‘A is invoked’);
}
}

class B extends A {}

class C extends A {
constructor() {
super();
}

//overrides the display function of A and hence behaves differently
display() {
console.log(‘C is invoked’);
}
}
var b = new B();
var c = new C();
b.display(); //output: :”A is invoked”
c.display(); //Output: “C is invoked”

Source:https://www.javatpoint.com/javascript-oops-polymorphism, www.stackOverflow.com

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

What is encapsulation?

A

Explanation: A tenet of OOP, it is the wrapping up of data under a single unit, those being variables, and functions acting on those variables. In JS this is accomplished with objects and constructor functions

Use: Its main benefit is it allows your code to be more readable, and robust against errors.

Example:
class Person {
#name = ‘Nathan’;

getName() {
return this.#name;
}

setName(name) {
this.#name = name;
}
}

Source:https://www.javatpoint.com/javascript-oops-encapsulation

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

What is a Linked List?

A

Explanation: It is a linear data structure similar to an array, except that elements are not stored on a particular memory location or index but instead represent separate objects containing a reference to the next item on the list Elements (commonly called nodes) contain two items: the data and the link to the next node. The data can be of any type. In a linked list, the head refers to the first node of the list. Null refers to the last node of the list. If the list is empty, the head is null. There are 3 Types: Singly, Doubly and Circular Linked List.

Use: The DOM, Blockchains, Prototypal Inheritance, Image Viewer, Music Payer and Previous And Next Page…

Example:
const linkedList = {
head: {
value: 1
next: {
value: 2
next: {
value: 12
next: {
value: 4
next: null
}
}
}
}
}
};

Source: Implementing A Linked List https://www.freecodecamp.org/news/implementing-a-linked-list-in-javascript/

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

What is a Doubly Linked List?

A

Explanation: It is pretty much the same as a linked list, only each node also has a pointer to the previous node and also has a null head pointer. It makes it easier to traverse the nodes and delete items, though they require more space, and take longer due to the extra pointers.

Use: They are used in stacks, hash tables, and binary trees.

Example:
const morningRoutine = {
value: ‘Make Bed’,
previous: null,
next: {
value: ‘Drink Tea’,
previous: <REFERENCE TO NODE MAKE BED>,
next: {
value: ‘Brush Teeth’,
previous: <REFERENCE TO NODE DRINK TEA>,
next: null,
},
},
};

Source: https://www.geeksforgeeks.org/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

Explanation: A Queue is a linear structure which follows a particular order in which the operations are performed. The order is First In First Out (FIFO).

Use: Queue is used when things don’t have to be processed immediately, but have to be processed in First In First Out order like Breadth First Search.

Example: The Event Loop Model prioritizes all the Jobs in a Job Queue.

Source: https://www.geeksforgeeks.org/queue-data-structure/

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

What is a Stack?

A

Explanation: A Stack is a linear data structure which follows a particular order in which the operations are performed. The order is LIFO(Last In First Out).

Use: Stacks are used to implement functions, parsers, expression evaluation, and backtracking algorithms.

Example: The Event loop uses call stack. Every time a script or function calls a function, it’s added to the top of the call stack. Every time the function exits, the interpreter removes it from the call stack.

Source: https://www.geeksforgeeks.org/stack-data-structure/

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