📖 Scenario: Imagine you are managing a stack of books on a table. You can only add or remove the top book. This is how a stack data structure works using the Last In, First Out (LIFO) principle.
🎯 Goal: You will create a stack using an array, add books to it, remove the top book, and then display the current stack.
📋 What You'll Learn
Create an array called stack with initial books
Create a variable called newBook to add to the stack
Add newBook to the top of the stack
Remove the top book from the stack
Print the current state of the stack
💡 Why This Matters
🌍 Real World
Stacks are used in real life when you pile plates or books where you only take the top one first.
💼 Career
Understanding stacks is important for programming tasks like undo features, expression evaluation, and managing function calls.
Progress0 / 4 steps
1
Create the initial stack
Create an array called stack with these exact books in order: 'Book1', 'Book2', 'Book3'
DSA Typescript
Hint
Use square brackets to create an array and include the books in the given order.
2
Add a new book to the stack
Create a variable called newBook and set it to 'Book4'
DSA Typescript
Hint
Use const newBook = 'Book4'; to create the variable.
3
Push the new book onto the stack and pop the top book
Use stack.push(newBook) to add newBook to the top of stack. Then use stack.pop() to remove the top book from stack
DSA Typescript
Hint
Use push to add and pop to remove the last item.
4
Print the current stack
Use console.log(stack) to print the current state of the stack
DSA Typescript
Hint
The stack should show the original three books after adding and removing the top book.
Practice
(1/5)
1. What does the LIFO principle mean in the context of a stack?
easy
A. Last In, First Out - the last element added is the first to be removed
B. Last In, First Out - the first element added is the first to be removed
C. First In, First Out - the first element added is the first to be removed
D. First In, Last Out - the first element added is the last to be removed
Solution
Step 1: Understand LIFO meaning
LIFO means the last element added to the stack is the first one to be removed.
Step 2: Match with options
Last In, First Out - the last element added is the first to be removed correctly describes this behavior as "Last In, First Out".
Final Answer:
Last In, First Out - the last element added is the first to be removed -> Option A
Quick Check:
LIFO = Last In, First Out [OK]
Hint: Remember stack removes newest item first [OK]
Common Mistakes:
Confusing LIFO with FIFO
Thinking first added is removed first
Mixing up the order of insertion and removal
2. Which of the following is the correct TypeScript syntax to push an element `5` onto a stack array named stack?
easy
A. stack.push(5);
B. stack.add(5);
C. stack.insert(5);
D. stack.append(5);
Solution
Step 1: Recall array method to add element at end
In TypeScript, the method to add an element at the end of an array is push().
Step 2: Verify options
Only stack.push(5); uses push(5), which is correct syntax to add 5 to the stack.
Final Answer:
stack.push(5); -> Option A
Quick Check:
Use push() to add element to stack [OK]
Hint: Use push() to add elements to stack [OK]
Common Mistakes:
Using non-existent methods like add or insert
Confusing push with append (not in JS/TS)
Syntax errors like missing parentheses
3. What will be the output of the following TypeScript code?