📖 Scenario: Imagine you are building a simple undo feature for a text editor. Each action is stored in a stack, so the last action can be undone first.
🎯 Goal: You will create a stack using an array, add a configuration for the stack size, implement push and pop operations, and finally display the stack's content.
📋 What You'll Learn
Create an integer array called stack with size 5
Create an integer variable called top and initialize it to -1
Write a method Push that adds an integer to the stack if there is space
Write a method Pop that removes and returns the top element if the stack is not empty
Print the current elements in the stack from bottom to top
💡 Why This Matters
🌍 Real World
Stacks are used in undo features, expression evaluation, and backtracking algorithms.
💼 Career
Understanding stack implementation helps in software development, debugging, and system design.
Progress0 / 4 steps
1
Create the stack array and top pointer
Create an integer array called stack with size 5 and an integer variable called top initialized to -1.
DSA C#
Hint
Use int[] stack = new int[5]; to create the array and int top = -1; to track the top.
2
Add a method to push elements onto the stack
Add a method called Push that takes an integer parameter value. Inside the method, check if top is less than 4, then increment top and assign value to stack[top].
DSA C#
Hint
Check if top is less than 4 before adding to avoid overflow.
3
Add a method to pop elements from the stack
Add a method called Pop that returns an integer. Inside the method, check if top is greater than or equal to 0, then return stack[top] and decrement top. If empty, return -1.
DSA C#
Hint
Return -1 if the stack is empty to indicate no elements to pop.
4
Print the current stack elements
Use a for loop with variable i from 0 to top inclusive to print each element in stack[i] separated by spaces.
DSA C#
Hint
Use a for loop from 0 to top and print each element with a space.
Practice
(1/5)
1. What is the main purpose of using an array in a stack implementation?
easy
A. To allow random access to elements anywhere in the stack
B. To store elements in a fixed-size, ordered collection
C. To automatically resize the stack when full
D. To sort elements as they are added
Solution
Step 1: Understand stack storage
A stack stores elements in order, last-in first-out, so it needs a structure to hold elements sequentially.
Step 2: Role of array in stack
An array provides a fixed-size, ordered collection to hold stack elements efficiently.
Final Answer:
To store elements in a fixed-size, ordered collection -> Option B
Quick Check:
Stack uses array for fixed order storage [OK]
Hint: Stack uses array for fixed order storage, not random access [OK]
Common Mistakes:
Thinking array resizes automatically
Confusing stack with queue behavior
Assuming array sorts elements
2. Which of the following is the correct way to declare an integer stack array of size 5 in C#?
easy
A. int[] stack = new int[5];
B. int stack = new int[5];
C. int[] stack = int[5];
D. stack int[] = new int(5);
Solution
Step 1: Recall C# array declaration syntax
In C#, arrays are declared with type[], variable name, and new type[size].
Step 2: Match correct syntax
int[] stack = new int[5]; matches this pattern: int[] stack = new int[5];
Final Answer:
int[] stack = new int[5]; -> Option A
Quick Check:
C# array declaration = int[] name = new int[size]; [OK]
Hint: Use type[] name = new type[size]; for arrays in C# [OK]
Common Mistakes:
Missing [] after type
Using parentheses instead of brackets
Incorrect variable declaration order
3. What will be the output after the following operations on a stack implemented using an array of size 3?
The popped values printed are 20, then 30, then 10.
Final Answer:
20 30 10 -> Option A
Quick Check:
Stack LIFO pop order = 20 30 10 [OK]
Hint: Pop prints last pushed element, follow LIFO order [OK]
Common Mistakes:
Confusing push and pop order
Assuming FIFO instead of LIFO
Missing last pop output
4. Identify the error in this stack push method code snippet in C#:
public void Push(int item) {
if (top == stack.Length) {
Console.WriteLine("Stack Overflow");
}
stack[top] = item;
top++;
}
medium
A. top should be compared with stack.Length - 1
B. top++ should be before assigning stack[top]
C. No return after overflow check causes overwrite
D. stack array should be resized when full
Solution
Step 1: Analyze overflow check logic
The code checks if top == stack.Length and prints overflow, but continues to assign and increment top anyway.
Step 2: Identify missing control flow after overflow
Without a return or exit after overflow message, the code overwrites stack[top] causing error.
Final Answer:
No return after overflow check causes overwrite -> Option C
Quick Check:
Overflow check must stop push to avoid overwrite [OK]
Hint: Stop push after overflow check to prevent overwrite [OK]
Common Mistakes:
Assuming top comparison off by one
Moving top++ before assignment causes wrong index
Ignoring need to stop after overflow
5. You want to implement a stack using an array that doubles its size automatically when full. Which approach correctly achieves this in C#?
hard
A. Increase the size of the existing array using stack.Resize() method
B. Overwrite oldest elements when stack is full to make space
C. Use a List<int> instead of array to avoid resizing manually
D. Create a new array double the size, copy elements, then assign it to the stack variable
Solution
Step 1: Understand array resizing in C#
Arrays have fixed size; to resize, create a new larger array and copy elements over.
Step 2: Evaluate options for resizing stack array
Create a new array double the size, copy elements, then assign it to the stack variable correctly describes creating a new array double the size, copying elements, and reassigning stack variable.
Final Answer:
Create a new array double the size, copy elements, then assign it to the stack variable -> Option D
Quick Check:
Resize array by new array + copy + assign [OK]
Hint: Arrays fixed size; resize by new array and copy [OK]
Common Mistakes:
Assuming arrays can resize in place
Using non-existent Resize method on arrays
Overwriting data instead of resizing
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.