Recursion & Stack
Advanced Functions: Recursion & Stack
What is recursion in JavaScript programming?
View Answer:
function pow(x, n) {
if (n == 1) {
return x;
} else {
return x * pow(x, n - 1); // calling pow(x, n) again on itself
}
}
What is the difference between recursive and iterative approaches in JavaScript?
View Answer:
function pow(x, n) {
return n == 1 ? x : x * pow(x, n - 1);
}
console.log(pow(2, 3)); // 8
What is the maximum (acceptable) number of recursive calls by JavaScript engines?
View Answer:
How does recursion work in JavaScript?
View Answer:
The following occurs when a function makes a nested call:
- The current function gets paused.
- The execution context gets remembered in a special data structure called the execution context stack.
- The nested call executes.
- After it ends, the old execution context gets retrieved from the stack, and the outer function resumes from where it stopped.
Can you explain what the execution context is in JavaScript?
View Answer:
What characteristics do the three tree traversal methods have in common? (Inorder, Preorder, and Postorder)
View Answer:
What is the difference between Backtracking and Recursion?
View Answer:
What is the definition of a recursive data structure?
View Answer:
let company = {
// the same object, compressed for brevity
sales: [
{ name: 'John', salary: 1000 },
{ name: 'Alice', salary: 1600 },
],
development: {
sites: [
{ name: 'Peter', salary: 2000 },
{ name: 'Alex', salary: 1800 },
],
internals: [{ name: 'Jack', salary: 1300 }],
},
};
What is the definition of a linked list?
View Answer:
Example: Linked List
let list = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: {
value: 4,
next: null,
},
},
},
};
// Alternative Implementation
let list = { value: 1 };
list.next = { value: 2 };
list.next.next = { value: 3 };
list.next.next.next = { value: 4 };
list.next.next.next.next = null;
When traversing a linked list, which approach is better, recursion or iterative?
View Answer:
let list = {
value: 1,
next: {
value: 2,
next: {
value: 3,
next: {
value: 4,
next: null,
},
},
},
};
// Iterative Approach
function printIteratively(list) {
let tmp = list;
while (tmp) {
console.log(tmp.value);
tmp = tmp.next;
}
}
printIteratively(list);
// Recursive Approach
function printRecursively(list) {
console.log(list.value); // output the current item
if (list.next) {
printRecursively(list.next); // do the same for the rest of the list
}
}
printRecursively(list);
What is a stack in JavaScript?
View Answer:
How can a stack be implemented in JavaScript?
View Answer:
class Stack {
constructor() {
this.items = [];
}
// Push element to the stack
push(element) {
this.items.push(element);
}
// Pop element from the stack
pop() {
if (this.items.length === 0) {
return 'Underflow';
}
return this.items.pop();
}
// Get the top element of the stack
peek() {
return this.items[this.items.length - 1];
}
// Check if the stack is empty
isEmpty() {
return this.items.length === 0;
}
// Get the size of the stack
size() {
return this.items.length;
}
}
const stack = new Stack();
What are the main operations in a stack?
View Answer:
What is a call stack in JavaScript?
View Answer:
Here is how it works:
When a script calls a function, the JavaScript engine pushes that function call onto the call stack and then starts carrying out the function.
If that function calls another function, that function is pushed onto the top of the call stack, and the JavaScript engine starts executing that function.
If a function finishes executing, the JavaScript engine pops it off the call stack and resumes execution where it left off in the last code listing.
If the stack takes up more space than it had assigned to it, it results in a "stack overflow" error.
The call stack is crucial for understanding how JavaScript works, especially its single-threaded, synchronous execution model. It's also vital for understanding more complex concepts like closures, callbacks, and promises.
Here's a simple example:
function functionOne() {
functionTwo();
}
function functionTwo() {
functionThree();
}
function functionThree() {
console.log('Hello, World!');
}
functionOne();
In this example, when functionOne
is called, it is pushed onto the call stack. Inside functionOne
, functionTwo
is called, and it gets pushed onto the call stack. This process continues until functionThree
is pushed onto the call stack.
Once functionThree
finishes execution (it logs "Hello, World!" to the console), it gets popped off the call stack. Then, functionTwo
gets popped, and finally functionOne
is popped from the stack, at which point the stack is empty, and the program ends.