Stack - DS Algo related questions

The Stack data structure is widely used in various programming scenarios, and interview questions often explore different applications of stacks. Here are some common programs where Stack is used, along with related interview questions:

1. Balanced Parentheses

  • Problem: Check if the given string of parentheses is balanced.
  • Question: Write a function to determine if the input string has balanced parentheses (e.g., {[()()]}).
  • Usage: Push opening brackets onto the stack and pop them when a closing bracket is encountered. At the end, if the stack is empty, the parentheses are balanced.

2. Reverse a String

  • Problem: Reverse a string using a stack.
  • Question: Given a string, reverse it using a stack data structure.
  • Usage: Push all characters of the string onto the stack and then pop them to get the reversed string.

3. Evaluate Postfix Expression

  • Problem: Evaluate an arithmetic expression written in postfix notation.
  • Question: Write a program to evaluate a postfix expression (e.g., 231*+9-).
  • Usage: Traverse the expression, push operands onto the stack, and pop for operations.

4. Infix to Postfix Conversion

  • Problem: Convert an infix expression to a postfix expression.
  • Question: Convert an infix expression (e.g., A+B*C) to postfix using a stack.
  • Usage: Operators are pushed onto the stack and output when appropriate precedence is met.

5. Next Greater Element

  • Problem: Find the next greater element for each element in an array.
  • Question: Given an array, find the next greater element for every element.
  • Usage: Traverse the array, use the stack to keep track of elements for which the next greater element hasn’t been found.

6. Stock Span Problem

  • Problem: Calculate the stock span values for each day.
  • Question: Given the price of stocks for N days, calculate the span of stocks.
  • Usage: Use the stack to keep track of previous days’ prices to compute the span.

7. Celebrity Problem

  • Problem: Find the celebrity in a party of N people.
  • Question: In a group of N people, a celebrity is known by everyone but knows no one. Find the celebrity using O(N) comparisons.
  • Usage: Utilize a stack to eliminate non-celebrities and identify the potential celebrity.

8. Design a Min Stack

  • Problem: Design a stack that supports push, pop, and retrieving the minimum element in constant time.
  • Question: Implement a stack that has a getMin() method returning the minimum element.
  • Usage: Use an auxiliary stack to keep track of the minimum values.

9. Check for Palindrome Using Stack

  • Problem: Check if a string is a palindrome.
  • Question: Determine whether a string is a palindrome by using a stack.
  • Usage: Push the first half of the string onto a stack and then compare with the second half.

10. Sort a Stack

  • Problem: Sort a stack in ascending order using another stack.
  • Question: Write a function to sort a stack using only push and pop operations.
  • Usage: Use a secondary stack to sort elements as they are popped from the original stack.

These are just some of the common scenarios where stacks are frequently used.

Comments

Popular posts from this blog

Full Stack Java Developer - FAQ

DSA Interview Coding Questions

Java Interview Question - New FAQs