Stacks - Interview Questions

Stacks are abstract data structures that allow access to only one data item - the last item inserted. Stacks are abstract since they internally use other data structures, such as arrays, and provide interfaces to restrict access to the items

Stacks are used in other data structures to perform certain operations. For example - Binary trees use stacks to help in traversing of nodes. Graphs use stacks to help in searching the vertices of the graph. Stacks can be used in many real-world scenarios and in solving interview questions. For example - Stacks can be used to process tasks, messages etc.

Hence, similar to other data structures, it is important to be thorough with the concepts of stacks, the common operations performed on stacks and the efficiency of these operations. Below interview questions on stacks address some of these topics. Java programming language will be used for code snippets when applicable to depict these concepts of Stacks.

What are stacks?

 

Stacks are programmatic data structures that allow access to only one item at a time, the last item inserted. Stacks internally use other data structures such as arrays for their implementation. A stack is a Last-In-First-Out (LIFO) storage mechanism, because the last item inserted is the first to be removed.

What are the key public interfaces provided by Stacks?

 

Stacks usually provide the following key interfaces.

push() - Inserts a new data item on the top of the stack.

pop() - Removes the item from the top of the stack

peek() - Returns the item from the top of the stack without removing it.

isFull() - Returns true if the stack is full.

isEmpty() - Returns true if the stack is empty.

Write a program to implement stack functionality?

 

Following Java program implements the stack functionality. It internally uses an array for the implementation.

class MyStack {

 private int maxSize;
 private int[] stackArray;
 private int top; //constructor
 public MyStack(int maxSize) {
  this.maxSize = maxSize;
  stackArray = new int[maxSize];
  top = -1;
 }

 //push an item to stack
 public void push(int i) {
  //increment top and insert item
  stackArray[++top] = i;
 }

 //remove an item from stack
 public void pop() {
  //return item and decrement top
  return stackArray[top--] = i;
 }

 //view item from top of stack
 public void peek() {
  return stackArray[top];
 }

 //check if stack is empty
 public void isEmpty() {
  return (top == -1);
 }

 //check if stack is full
 public void isFull() {
  return (top == maxSize);
 }
}

What is the time efficiency of the push() and pop() operations of stacks?

 

The push() and pop() methods implemented in stacks take constant time to execute. i.e. the time to execute is very quick and is not dependent on the number of items on the stack. In Big O notation this is represented as O(1).

What are some common scenarios where stacks are used?

 

Following are some of the common scenarios in which Stacks can be Used.

- To check if delimiters (parenthesis, brackets etc.) are balanced in a computer program

- To reverse strings

- To traverse the nodes of a binary tree.

- To search the vertices of a graph.

- To process tasks

- To process messages

MASTER THE CODING INTERVIEW: DATA STRUCTURES + ALGORITHMS - TOP SELLING COURSE ON UDEMY.
 
GO TO COURSE
 
 
Subscribe to our Questions

 

Data Structures - Interview Questions

ArraysLinked ListsStacksQueuesBinary TreesRed-Black Trees2-3-4 TreesGraphsTrie
 
MASTER Data Structures  

Top ranked courses to help you master Data Structures skills.
Master the Coding Interview: Data Structures + Algorithms

iconicon

Offered By - Andrei Neagoie
Platform - Udemy
Rating - * * * * *
Students Enrolled - 100,000 +

RECOMMENDED RESOURCES
Behaviorial Interview
Top resource to prepare for behaviorial and situational interview questions.

STAR Interview Example