
Practice 50 JavaScript Stack Programming Questions
Q1. Write a JavaScript program to implement a stack using an array.
Input: push(10), push(20), push(30)
Expected Output: [10, 20, 30]
Q2. Write a JavaScript program to push an element to the stack.
Input: stack = [1, 2, 3], push(4)
Expected Output: [1, 2, 3, 4]
Q3. Write a JavaScript program to pop an element from the stack.
Input: stack = [1, 2, 3, 4], pop()
Expected Output: [1, 2, 3]
Q4. Write a JavaScript program to check if a stack is empty.
Input: stack = [1, 2, 3]
Expected Output: false
Q5. Write a JavaScript program to get the top element of the stack without popping it.
Input: stack = [1, 2, 3, 4]
Expected Output: 4
Q6. Write a JavaScript program to check if the stack contains a specific element.
Input: stack = [1, 2, 3, 4], element = 3
Expected Output: true
Q7. Write a JavaScript program to get the size of the stack.
Input: stack = [1, 2, 3, 4]
Expected Output: 4
Q8. Write a JavaScript program to clear the stack.
Input: stack = [1, 2, 3, 4]
Expected Output: []
Q9. Write a JavaScript program to reverse a string using a stack.
Input: string = "hello"
Expected Output: "olleh"
Q10. Write a JavaScript program to implement the stack's push and pop operations using two arrays.
Input: push(1), push(2), pop()
Expected Output: [1]
Q11. Write a JavaScript program to implement a stack and perform multiple push and pop operations.
Input: push(1), push(2), pop(), push(3)
Expected Output: [1, 3]
Q12. Write a JavaScript program to implement a stack with a maximum size.
Input: maxSize = 3, push(1), push(2), push(3), push(4)
Expected Output: [1, 2, 3] (4 is not pushed due to size limit)
Q13. Write a JavaScript program to implement a stack with a peek operation.
Input: stack = [1, 2, 3]
Expected Output: peek() = 3
Q14. Write a JavaScript program to check if a stack is full (using a max size).
Input: stack = [1, 2, 3], maxSize = 3
Expected Output: true
Q15. Write a JavaScript program to simulate the undo operation using a stack.
Input: push(1), push(2), undo(), push(3)
Expected Output: [1, 3]
Q16. Write a JavaScript program to implement a stack that can handle both integers and strings.
Input: push(10), push("hello"), push(20)
Expected Output: [10, "hello", 20]
Q17. Write a JavaScript program to check if an expression has balanced parentheses using a stack.
Input: expression = "(a + b) * (c + d)"
Expected Output: true
Q18. Write a JavaScript program to evaluate a postfix expression using a stack.
Input: expression = "23*5+"
Expected Output: 11
Q19. Write a JavaScript program to evaluate a prefix expression using a stack.
Input: expression = "+23*5"
Expected Output: 11
Q20. Write a JavaScript program to implement a stack using linked list.
Input: push(1), push(2), push(3)
Expected Output: [1, 2, 3] (linked list structure)
Q21. Write a JavaScript program to perform a depth-first traversal of a graph using a stack.
Input: graph = {1: [2, 3], 2: [4, 5], 3: [6]}
Expected Output: [1, 3, 6, 2, 5, 4]
Q22. Write a JavaScript program to check if a given string of brackets is valid.
Input: str = "{[()]}"
Expected Output: true
Q23. Write a JavaScript program to implement the stack's push and pop operations using recursion.
Input: push(1), push(2), pop()
Expected Output: [1]
Q24. Write a JavaScript program to reverse an array using a stack.
Input: arr = [1, 2, 3, 4]
Expected Output: [4, 3, 2, 1]
Q25. Write a JavaScript program to simulate a stack-based calculator that handles addition, subtraction, multiplication, and division.
Input: expression = "3 4 + 2 *"
Expected Output: 14
Q26. Write a JavaScript program to find the next greater element for every element in an array using a stack.
Input: arr = [4, 5, 2, 10]
Expected Output: [5, 10, 10, -1]
Q27. Write a JavaScript program to find the previous greater element for every element in an array using a stack.
Input: arr = [4, 5, 2, 10]
Expected Output: [-1, -1, 5, -1]
Q28. Write a JavaScript program to find the largest rectangle area in a histogram using a stack.
Input: histogram = [2, 1, 5, 6, 2, 3]
Expected Output: 10
Q29. Write a JavaScript program to find the longest valid parentheses substring using a stack.
Input: str = "(()())"
Expected Output: 6
Q30. Write a JavaScript program to find the maximum area of a rectangle that can be formed with a given set of bars using a stack.
Input: bars = [1, 3, 2, 1, 2]
Expected Output: 5
Q31. Write a JavaScript program to find the largest number that can be obtained by reversing a stack.
Input: stack = [5, 3, 8, 1]
Expected Output: 8
Q32. Write a JavaScript program to implement a stack that supports retrieving the minimum element.
Input: push(5), push(2), min()
Expected Output: 2
Q33. Write a JavaScript program to implement a stack that supports retrieving the maximum element.
Input: push(5), push(8), max()
Expected Output: 8
Q34. Write a JavaScript program to implement a stack that supports retrieving the median element.
Input: push(3), push(1), push(2), median()
Expected Output: 2
Q35. Write a JavaScript program to implement a queue using two stacks.
Input: enqueue(1), enqueue(2), dequeue()
Expected Output: 1
Q36. Write a JavaScript program to simulate a stack with limited size.
Input: maxSize = 3, push(1), push(2), push(3), push(4)
Expected Output: [1, 2, 3]
Q37. Write a JavaScript program to implement a stack-based expression evaluator for infix notation.
Input: expression = "(3 + 2) * 5"
Expected Output: 25
Q38. Write a JavaScript program to find the maximum element in a stack without using any additional memory.
Input: stack = [1, 5, 3, 4]
Expected Output: 5
Q39. Write a JavaScript program to implement a stack with an additional operation to check if an element is present.
Input: push(10), push(20), contains(10)
Expected Output: true
Q40. Write a JavaScript program to implement a stack with a special operation to duplicate the top element.
Input: push(1), push(2), duplicateTop()
Expected Output: [1, 2, 2]
Q41. Write a JavaScript program to implement a stack with an additional operation to remove the top element.
Input: push(1), push(2), removeTop()
Expected Output: [1]
Q42. Write a JavaScript program to implement a stack-based sorting algorithm.
Input: stack = [3, 1, 2]
Expected Output: [1, 2, 3]
Q43. Write a JavaScript program to simulate the 'back' button functionality using a stack.
Input: visit("google.com"), visit("facebook.com"), back()
Expected Output: "google.com"
Q44. Write a JavaScript program to simulate a browser history using a stack.
Input: visit("google.com"), visit("facebook.com"), history()
Expected Output: ["google.com", "facebook.com"]
Q45. Write a JavaScript program to simulate a stack-based file navigation system.
Input: open("file1"), open("file2"), back()
Expected Output: "file1"
Q46. Write a JavaScript program to implement a stack that can sort elements as they are pushed.
Input: push(3), push(1), push(2)
Expected Output: [1, 2, 3]
Q47. Write a JavaScript program to implement a stack that supports clearing elements conditionally.
Input: push(1), push(2), push(3), clearIfEven()
Expected Output: [1, 3]
Q48. Write a JavaScript program to implement a stack-based calculator for evaluating postfix expressions.
Input: expression = "3 4 + 2 *"
Expected Output: 14
Q49. Write a JavaScript program to perform a depth-first search on a tree using a stack.
Input: tree = {1: [2, 3], 2: [4, 5], 3: [6]}
Expected Output: [1, 3, 6, 2, 5, 4]
Q50. Write a JavaScript program to simulate the push and pop operations of a stack with a visual output.
Input: push(1), push(2), pop()
Expected Output: Push: 1, 2, Pop: 2