📖 Scenario: Imagine you are organizing a stack of plates in a kitchen. You can only add or remove the top plate. This is how a stack data structure works in programming.Stacks help solve problems where the last thing added is the first thing you need to remove, like undo actions in apps or checking matching brackets in code.
🎯 Goal: You will create a simple stack using an array in TypeScript. You will add items, remove the top item, and see how the stack changes step-by-step.
📋 What You'll Learn
Create an empty stack as an array called stack
Add three items to the stack in order: 'plate1', 'plate2', 'plate3'
Remove the top item from the stack and store it in a variable called removedPlate
Print the current state of the stack after removal
💡 Why This Matters
🌍 Real World
Stacks are used in web browsers to keep track of pages you visit so you can go back in order.
💼 Career
Understanding stacks helps in coding interviews and building features like undo buttons or expression evaluators.
Progress0 / 4 steps
1
Create an empty stack
Create an empty array called stack to represent the stack.
DSA Typescript
Hint
Use const stack: string[] = []; to create an empty array for the stack.
2
Add three plates to the stack
Add 'plate1', 'plate2', and 'plate3' to the stack array using the push method in this order.
DSA Typescript
Hint
Use stack.push('plate1'); and similarly for the other plates to add them to the stack.
3
Remove the top plate from the stack
Remove the top item from stack using pop() and store it in a variable called removedPlate.
DSA Typescript
Hint
Use const removedPlate = stack.pop(); to remove the top plate and save it.
4
Print the current stack
Print the current state of stack using console.log(stack).
DSA Typescript
Hint
Use console.log(stack); to see the stack after removing the top plate.
Practice
(1/5)
1. Why do we use a stack data structure in programming?
easy
A. To sort numbers in ascending order automatically
B. To store data in random order for quick access
C. To keep track of function calls and return in the correct order
D. To store large amounts of data permanently
Solution
Step 1: Understand the purpose of a stack
A stack stores items in Last In First Out (LIFO) order, which is perfect for tracking function calls.
Step 2: Relate stack to function calls
When a function calls another, the current function is paused and saved on the stack. When the called function finishes, the program returns to the last saved function.
Final Answer:
To keep track of function calls and return in the correct order -> Option C
Quick Check:
Stack = function call tracking [OK]
Hint: Stack follows LIFO, perfect for tracking nested calls [OK]
Common Mistakes:
Thinking stack stores data randomly
Confusing stack with queue
Assuming stack sorts data automatically
2. Which of the following is the correct way to push an element '5' onto a stack named myStack in TypeScript?
easy
A. myStack.push(5);
B. myStack.add(5);
C. myStack.insert(5);
D. myStack.enqueue(5);
Solution
Step 1: Recall stack method names in TypeScript
Stacks use push() to add elements on top.
Step 2: Identify correct method for adding element
push() is the standard method; others like add(), insert(), or enqueue() are not used for stacks.
Final Answer:
myStack.push(5); -> Option A
Quick Check:
Push adds element on top [OK]
Hint: Use push() to add on stack top [OK]
Common Mistakes:
Using enqueue which is for queues
Using add or insert which are not stack methods
Confusing push with pop
3. What will be the output of the following TypeScript code?