Practice
Resources
Contests
Online IDE
New
Free Mock
Events New Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
logo
Events
Attend free live masterclass hosted by top tech professionals
New
Scaler
Explore Offerings by SCALER

Stack & Queue

Stack

Stacks are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container.

Stacks are implemented as container adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements.
Elements are pushed/popped from the “back” of the specific container, which is known as the top of the stack

The functions associated with stack are:

  • Declaration
    stack<int>s; (creates an empty stack of integers)
  • Size:
    int size = s.size(); // Gives the size of stack
  • top()
    s.top() //Returns a reference to the top most element of the stack
  • Pushing an integer into a stack:
    s.push(x); //(where x is an integer.The size increases by 1 after this.)
               //Adds the element x at the top of the stack        
    
  • Popping the element from the stack:
    s.pop(); //Deletes the top most element of the stack. (After this the size decreases by 1)
  • Empty:
    s.empty() //Returns a boolean value which tells whether the stack is empty or not

Example

stack <int> s;
s.push(5);
s.push(3);
s.push(10);
s.push(3);
cout<<s.top()<<endl;
// prints 3
cout<<s.empty()<<endl;
// prints 0, since it's false
s.pop();
cout<<s.top()<<endl;
// prints 10

Queue

Queues are a type of container adaptors which operate in a first in first out (FIFO) type of arrangement. Elements are inserted at the back (end) and are deleted from the front.

The functions supported by queue are:

  • Declaration
    queue<int>q; (creates an empty queue of integers)
  • Size:
    int size = q.size(); // Gives the size of queue
  • front() and back()
    q.front() // Returns a reference to the first element of the queue
    q.back() // Returns a reference to the last element of the queue
  • Pushing an integer into a queue:
    q.push(x); //(where x is an integer.The size increases by 1 after this.)
               // Adds the element x at the end of the queue        
    
  • Popping the element from the queue:
    q.pop(); // Deletes the first element of the queue. (After this the size decreases by 1)
  • Empty:
    q.empty() //Returns a boolean value which tells whether the queue is empty or not

Example

queue <int> q;
q.push(5);
q.push(3);
q.push(10);
q.push(3);
cout<<q.front()<<endl;
// prints 5
cout<<q.empty()<<endl;
// prints 0, since it's false
q.pop();
cout<<q.front()<<endl;
// prints 3

Deque

Double ended queue or Deque(part of C++ STL) are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back). The member functions of deque that are mainly used are:

  • Declaration
    deque mydeque; //Creates a double ended queue of deque of int type
  • Size:
    int size = mydeque.size(); // Gives the size of the deque 
  • front() and back()
    mydeque.front() // Returns a reference to the first element of the deque
    mydeque.back() // Returns a reference to the last element of the deque
  • Pushing an integer into a Deque:
    mydeque.push_back(1); //Pushes element at the end
    mydeque.push_front(2); //Pushes element at the beginning
  • Popping the element from the Deque:
    mydeque.pop_back(); //Pops element from the end
    mydeque.pop_front(); //Pops element from the beginning
  • Empty:
    mydeque.empty() //Returns a boolean value which tells whether the deque is empty or not

Example

deque <int> dq;
dq.push_front(5); // dq = [5]
dq.push_back(3); // dq = [5, 3]
dq.push_back(10); // dq = [5, 3, 10]
dq.push_front(3); // dq = [3, 5, 3, 10]
cout<<dq.front()<<endl;
// prints 3
cout<<dq.empty()<<endl;
// prints 0, since it's false
dq.pop_front();
cout<<dq.front()<<endl;
// prints 5

Try the following example in the editor below.

Given an array of integers A. There is a sliding window of size B which is moving from the very left of the array to the very right.
You can only see the B numbers in the window. Each time the sliding window moves rightwards by one position.
You have to find the maximum for each window and print it in a new line.
Return an array C, where C[i] is the maximum value in the array from A[i] to A[i+B-1].
Refer to the given example for clarity.

Constraints

1 <= B <= |A|<= 106

Input Format

The first argument given is the integer array A.
The second argument given is the integer B.

Output Format

Return an array C, where C[i] is the maximum value of from A[i] to A[i+B-1].

Sample Input

A = [1, 3, -1, -3, 5, 3, 6, 7]
B = 3

Sample Output

[3, 3, 5, 5, 6, 7]
Start solving Stack & Queue on Interview Code Editor
Hints
  • Complete Solution

Discussion


Loading...
Click here to start solving coding interview questions
Free Mock Assessment
Fill up the details for personalised experience.
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Graduation Year *
Graduation Year *
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
*Enter the expected year of graduation if you're student
Current Employer
Company Name
College you graduated from
College/University Name
Job Title
Job Title
Engineering Leadership
Software Development Engineer (Backend)
Software Development Engineer (Frontend)
Software Development Engineer (Full Stack)
Data Scientist
Android Engineer
iOS Engineer
Devops Engineer
Support Engineer
Research Engineer
Engineering Intern
QA Engineer
Co-founder
SDET
Product Manager
Product Designer
Backend Architect
Program Manager
Release Engineer
Security Leadership
Database Administrator
Data Analyst
Data Engineer
Non Coder
Other
Please verify your phone number
Edit
Resend OTP
By clicking on Start Test, I agree to be contacted by Scaler in the future.
Already have an account? Log in
Free Mock Assessment
Instructions from Interviewbit
Start Test