February 28, 2020

# Stack Interview Questions

The stack is always a favorite part of the interviewer in any programming interview.

Stack has an important place in Linear data structure and used to implement complex solutions.

Stack Interview Questions are the best collection of concepts and questions asked in the data structure Interview.

While keeping in mind the practical implementation and complexity we have given plenty of examples of stack implementation and operations using C Programming.

## What is a Stack?

A stack is a linear data structure and represented a list of the element in which an element may be inserted or deleted only from one end, called the top of the stack.

Elements in the stack are arranged in a linear fashion. Unlike an array, we can not access an element randomly.

According to the behavior of Stack, we can have two important operations

1. PUSH and
2. POP

Insertion of an element in the stack from the top is called PUSH and removal of an element from the stack is called POP.

We can only access the last element inserted in a Stack. Hence We can say an Item can either Inserted or Deleted At only one end of the stack and that end is the TOP. This is why We called it a LIFO – Last In First Out.

## Main Features of Stack

1. The stack is a Linear type data structure
2. Allow only the last element to access
3. Random access of element is not allowed
4. The stack is an ordered list of similar data type
5. When a stack is completely full it is called Overflow Stack and If completely empty it is called Underflow Stack.
6. A stack is a recursive data structure. Here is a structural definition of a Stack:
1. a stack is either empty or
2. it consisted of a top and the rest which is a stack;

## Application of Stack

• We can use stack reverse a string. You push a given string to stack – character by character – and then POP character from the stack.
• Undo Operations in the text editor is an application of stack PUSH and POP.

## Basic Operations on Stack

Stack allowed the following operation with data stored it with:

1. Push operation
2. Pop operation
3. Peek operation
4. isFull
5. isEmpty

## Algorithm for PUSH Operation in Stack

Consider MAX represents the last position of the element to be inserted and TOP is the current position. TOP initially points to 0.

## Algorithm POP Operation in Stack

The function will return the popped value from the stack

## Implementation of Stack

The stack can be implemented as

1. Using Array
2. Using a Linked list

Array and linked list both are used to implement a stack. Both have some advantages and disadvantages associated with them. Here are some relative comparisons between these two.

• The array is a collection of similar data types elements while the linked list is a collection of elements of the same type connected to each other by pointers.
• Array consumes memory in a contiguous manner. Also, the size of array is decided at compile time. This kind of memory allocation is called Static memory allocation. But in case of linked list memory allocated at runtime. The size of the list can be increased or decreased at runtime. This kind of memory allocation is called dynamic memory allocation.
• The size of the array is fixed. Memory allocated as soon as the array is declared. In the case of a linked list, size can grow or shrink.
• In array, due to memory allocation in contiguous in manner, insertion and deletion take more time in comparison to linked list.
• Accessing an element in an array is faster. Also, any element of an array can be easily accessed by using indexing. This is not in the case of a linked list. You have to transverse from the starting point to access any element.

## Implement Stack using an Array

A working Example of Implementation of Stack using Array

## Implement Stack using a Linked list

A working Example of Implementation of Stack using Linked List

#### Sudhir Singh

An Electronics Engineer and an IOT programmer having 5+ years of experience.

View all posts by Sudhir Singh →

## 9 thoughts on “Stack Interview Questions”

1. Dallas Rathrock says:

Hi would you mind letting me know which web host you’re utilizing? I’ve loaded your blog in 3 completely different internet browsers and I must say this blog loads a lot quicker then most. Can you suggest a good hosting provider at a fair price? Many thanks, I appreciate it!|

2. oprol evorter says:

Outstanding post, I think blog owners should learn a lot from this blog it is really user-friendly.

3. Kate Holtzman says:

Excellent publish, very informative. I’m wondering why the other specialists of this sector do not realize this. You must proceed your writing. I’m sure, you have a great readers’ base already

4. Shakita Picco says:

Why users still make use of to read news papers when in this technological world everything is existing on web?

1. Thanks for the question, Both the mediums have their own importance and usability, Although most of the things are now available in digital platforms.

5. Leandro Dicostanzo says:

Fantastic goods from you, man. I’ve understand your stuff previous to and you are just extremely great. I really like what you have acquired here, certainly like what you are saying and the way in which you say it. You make it entertaining and you still take care of to keep it sensible. I can not wait to read far more from you. This is actually a wonderful site.

1. Great to hear that from one of our visitors, keep motivating us. Thanks.

This site uses Akismet to reduce spam. Learn how your comment data is processed.