STACK OPERATIONS USING ARRAY
1. Push Operation in Stack (Array)
Problem:
Push an element onto the stack implemented using an array.
Sample Input:
Initial Stack: [10, 20, 30]
Element to push: 40
Sample Output:
Updated Stack: [10, 20, 30, 40]
2. Pop Operation in Stack (Array)
Problem:
Pop the top element from a stack implemented using an array.
Sample Input:
Stack: [10, 20, 30, 40]
Sample Output:
Popped Element: 40
Updated Stack: [10, 20, 30]
3. Peek Operation in Stack (Array)
Problem:
Return the top element of the stack without removing it.
Sample Input:
Stack: [10, 20, 30]
Sample Output:
Top Element: 30
4. Check if Stack is Empty (Array)
Problem:
Check whether the stack is empty.
Sample Input:
Stack: []
Sample Output:
true
5. Stack Overflow Check (Fixed Size Array)
Problem:
If the array is full, do not allow pushing new elements (overflow).
Sample Input:
Stack Capacity: 3
Current Stack: [10, 20, 30]
Element to push: 40
Sample Output:
Stack Overflow
STACK OPERATIONS USING LINKED LIST
1. Push Operation in Stack (Linked List)
Problem:
Insert an element at the top of a stack using a linked list.
Sample Input:
Initial Stack: 10 → 20 → 30
Element to push: 40
Sample Output:
Stack after push: 40 → 10 → 20 → 30
2. Pop Operation in Stack (Linked List)
Problem:
Remove the top element from a stack using linked list.
Sample Input:
Stack: 40 → 10 → 20 → 30
Sample Output:
Popped Element: 40
Updated Stack: 10 → 20 → 30
3. Peek Operation in Stack (Linked List)
Problem:
Return the top element without removing it.
Sample Input:
Stack: 10 → 20 → 30
Sample Output:
Top Element: 10
4. Check if Stack is Empty (Linked List)
Problem:
Check whether the stack is empty.
Sample Input:
Stack: null
Sample Output:
true
PROBLEMS ON STACK
[Link] a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input
string is valid.
An input string is valid if:
1. Open brackets must be closed by the same type of brackets.
2. Open brackets must be closed in the correct order.
3. Every close bracket has a corresponding open bracket of the same type.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Example 4:
Input: s = "([])"
Output: true
2. You are keeping the scores for a baseball game with strange rules. At the beginning of the
game, you start with an empty record.
You are given a list of strings operations, where operations[i] is the ith operation you must
apply to the record and is one of the following:
An integer x.
Record a new score of x.
'+'.
Record a new score that is the sum of the previous two scores.
'D'.
Record a new score that is the double of the previous score.
'C'.
Invalidate the previous score, removing it from the record.
Return the sum of all the scores on the record after applying all the operations.
The test cases are generated such that the answer and all intermediate calculations fit in a 32-
bit integer and that all operations are valid.
Example 1:
Input: ops = ["5","2","C","D","+"]
Output: 30
Explanation:
"5" - Add 5 to the record, record is now [5].
"2" - Add 2 to the record, record is now [5, 2].
"C" - Invalidate and remove the previous score, record is now [5].
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
The total sum is 5 + 10 + 15 = 30.
Example 2:
Input: ops = ["5","-2","4","C","D","9","+","+"]
Output: 27
Explanation:
"5" - Add 5 to the record, record is now [5].
"-2" - Add -2 to the record, record is now [5, -2].
"4" - Add 4 to the record, record is now [5, -2, 4].
"C" - Invalidate and remove the previous score, record is now [5, -2].
"D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
"9" - Add 9 to the record, record is now [5, -2, -4, 9].
"+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
"+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
Example 3:
Input: ops = ["1","C"]
Output: 0
Explanation:
"1" - Add 1 to the record, record is now [1].
"C" - Invalidate and remove the previous score, record is now [].
Since the record is empty, the total sum is 0.
3. Given two strings s and t, return true if they are equal when both are typed into empty text
editors. '#' means a backspace character.
Note that after backspacing an empty text, the text will continue empty.
Example 1:
Input: s = "ab#c", t = "ad#c"
Output: true
Explanation: Both s and t become "ac".
Example 2:
Input: s = "ab##", t = "c#d#"
Output: true
Explanation: Both s and t become "".
Example 3:
Input: s = "a#c", t = "b"
Output: false
Explanation: s becomes "c" while t becomes "b".
[Link] a string using stack logic (LIFO) without using Java's built-in collections.
Sample Input:world
Sample Output:dlrow
[Link] a program to implement a stack that supports the following operations:
push(x) – Push an element x onto the stack
getMiddle() – Return the middle element of the stack in O(1) time
NOTE : For an even number of elements, return the second middle element (i.e., n/2
+ 1 from the top).
Sample Input : 10 20 30 40 50
Sample Output : 30
[Link] a program to perform the following operations on a stack:
push(x) – Push elements into the stack
deleteMiddle() – Delete the middle element of the stack
NOTE : For an even number of elements, delete the second middle element (n/2 + 1
from the top).
Sample Input : 10 20 30 40 50
Sample Output : 50 40 20 10
[Link] All Occurrences of a Given Element (Without Collections)
Sample Input:
Stack: 5 1 2 5 3 5
Element to remove: 5
Sample Output:
Updated Stack: 1 → 2 → 3
[Link] if a Stack is Palindrome (Without Collections)
Sample Input: r a d a r
Sample Output:true
[Link] the Minimum Element in a Stack (Without Collections)
Sample Input:5 2 8 1 3
Sample Output:1
[Link] the Maximum Element in a Stack (Without Collections)
Sample Input:5 2 8 1 3
Sample Output:8
[Link] a program to evaluate a valid postfix expression using a stack implemented with an
array.
Sample Input:
Postfix Expression: 5 3 2 * +
Sample Output:
Result: 11