[Hackerrank] – Queue using two stacks

    by nikoo28

    A queue is a linear data structure which maintains the order in which the elements appear. You need to implement a queue, using two stacks such that it behaves in the same way.

    If you are unfamiliar with the queue data structure and the stack data structure, it would be a good idea to learn them before approaching this problem.

    Problem Statement:

    The basic crux of the problem is that you are to implement a queue and its operations using stacks. It may seem unnecessary in the beginning, as a data structure for queue already exists. Sometimes you have a limited architecture and still need a solution that supports your use case.

    If you look at the operations will still behave like a queue:

    enqueue ( 4 )
    enqueue ( 8 )
    enqueue ( 15 )
    enqueue ( 16 )
    dequeue ( ) => 4
    dequeue ( ) => 8
    peek ( ) => 15
    dequeue ( ) => 15Code language: PHP (php)

    For all the operations the desired output is same as if you are operating on a queue.

    figure showing operations on an actual queue
    Fig: Actual queue operation

    Solution:

    This problem does not have a brute force method. Just a common understanding of how both the data structures work should be helpful in approaching this. Just need to make sure that you are following the FIFO (First in First out). Let us see how that would look.

    Let us start by creating 2 stacks as desired. One stack is In and the other is named as Out. Apart from those, also create a queue that is mimicking the actual operations.

    Queue:
    --------------------------------------------------------------------------
          |              |              |              |            |
    --------------------------------------------------------------------------
    
    Stacks:
    
    |           |                       |           |
    -------------                       -------------
    |           |                       |           |
    -------------                       -------------
    |           |                       |           |
    -------------                       -------------
    |           |                       |           |
    -------------                       -------------
         In                                 OutCode language: plaintext (plaintext)

    Enqueue:

    Now, let us try to perform 3 operations on the queue:

    - enqueue ( 4 )
    - enqueue ( 8 )
    - enqueue ( 15 )Code language: Java (java)

    For every enqueue( ) operation, you push( ) the element in the In stack. As per this our data structures would look like:

    Queue:
    --------------------------------------------------------------------------
          |              |              |       15      |      8     |   4
    --------------------------------------------------------------------------
    
    Stacks:
    
    |           |                       |           |
    -------------                       -------------
    |    15     |                       |           |
    -------------                       -------------
    |    8      |                       |           |
    -------------                       -------------
    |    4      |                       |           |
    -------------                       -------------
         In                                 OutCode language: plaintext (plaintext)

    This takes care of one part of the problem. How do you remove elements now? If you perform a dequeue () operation on the queue, you would get the value 4. However, if you observe carefully you cannot extract the value 4 from the In stack. As soon as you do a pop(), you would get the element 15. This is not what we desired, it breaks the rules of a queue.

    That is where the Out stack comes in handy. As soon as you receive a dequeue () request, check the Out stack. If it is not empty, do a pop() operation and return the element. If it is empty. Transfer all the elements from In stack to the Out stack. Let us see how that works.

    Dequeue:

    It is a 2-step process. First we pop() all the elements from In stack and push() it in the Out stack.

    Queue:
    --------------------------------------------------------------------------
          |              |              |       15      |      8     |   4
    --------------------------------------------------------------------------
    
    Stacks:
    
    |           |                       |           |
    -------------                       -------------
    |           |                       |    4      |
    -------------                       -------------
    |           |                       |    8      |
    -------------                       -------------
    |           |                       |    15     |
    -------------                       -------------
         In                                 Out

    Now, if you look closely stack Out looks very similar to the queue. If you pop an element from it, you will get the same element that you will get from the queue.

    If we now perform a dequeue operation, we just need to pop an element from the other stack. The data structures now look like:

    Queue:
    --------------------------------------------------------------------------
          |              |              |       15      |      8     |   
    --------------------------------------------------------------------------
    
    Stacks:
    
    |           |                       |           |
    -------------                       -------------
    |           |                       |           |
    -------------                       -------------
    |           |                       |    8      |
    -------------                       -------------
    |           |                       |    15     |
    -------------                       -------------
         In                                 Out

    Algorithm:

    We can now write the algorithm for all the 3 operations:

    • Create two stacks In and Out
    • enqueue( ) : Push elements in stack In
    • dequeue( ) : If stack Out is not empty, pop an element from the stack. If the stack is empty, pop all elements of stack In in the Out stack and then again pop from Out.
    • peek ( ) : Same as dequeue, but instead of pop just perform the peek operation.

    Code:

    private Stack input = new Stack();
    private Stack output = new Stack();
    
    public void enqueue(int x) {
      input.push(x);
    }
    
    public int dequeue() {
      peek();
      return output.pop();
    }
    
    public int peek() {
      if (output.empty())
        while (!input.empty())
          output.push(input.pop());
      return output.peek();
    }
    
    public boolean isEmpty() {
      return input.empty() && output.empty();
    }
    Code language: Java (java)

    The problem statement can be found at HackerRank : Queue using two Stacks.
    You can also find the complete solution and the test cases on GitHub as well.

    Video Explanation:

    YouTube player
    0 comments
    0 FacebookTwitterLinkedinWhatsappEmail
  • ArraysTheory

    Array Data Structure

    by nikoo28
    5 minutes read

    An array is the most basic data structure one can think of. It may seem very easy to use and in a lot of my posts we have been solving problems using arrays. However, if you are just getting started with programming this post is probably for you. I would like to cover some of the basic concepts that makes this data structure so desirable and easy to use. If you have been programming for a while now this post is probably not written for you. Read along keeping that …

    1 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given a string, Sherlock considers it valid if all the characters in the string occur the same number of time. However, a string is also valid if the frequencies are same after removing any one character. Example 1:Input: str = “aabbcd”Output: NO Example 2:Input: str = “abcc”Output: YES Problem Statement: Let us try to simplify the problem statement first. Sherlock needs to verify if a given string is valid. A string is valid under 2 conditions: All characters of the string appear the same number of times. If you …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    [Hackerrank] – Two Strings Solution

    by nikoo28
    4 minutes read

    Question: Given two strings, determine if they share a common sub-string. Input: “a” and “art” Output: YES Problem Statement Let me try to simplify this problem statement first. You are given two strings, and you need to find out if they share a common sub-string or not. If you can find a common sub-string, output “YES”, else output “NO”. Note that you don’t have to find out what is the common sub-string. Another important aspect of this problem is that the sub-string can be as small as 1 character. A …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    [Hackerrank] – Between Two Sets Solution

    by nikoo28
    5 minutes read

    Question: You will be given two arrays of integers and asked to determine all integers between two sets that satisfy the following two conditions:– The elements of the first array are all factors of the integer being considered– The integer being considered is a factor of all elements of the second array Input:a = { 2, 6 }b = { 24, 36 } Output:2 I really want to simplify this really confusing problem statement first. The hardest part about this problem is to understand what is it actually saying. To …

    1 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    [Hackerrank] – Pairs Solution

    by nikoo28
    5 minutes read

    Question: Given an array of integers, find the number of pairs of array elements that have a difference equal to the target value. Example: Input:arr[ ] = {1, 2, 3, 4}k = 1 Output: 3 Problem Statement Let us try to simplify the problem statement first and understand the sample test case. You are given an array of unique integers which is in any random order. Along with the array, you are also given a target value k. If you pick up any 2 integers from the array, they would …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    [Hackerrank] – Missing Numbers Solution

    by nikoo28
    4 minutes read

    Question: You are required to find missing numbers that are left out while an artist transports numbers from one array to other. The output array should be sorted. Input:arr [ ] = {7, 2, 5, 3, 5, 3}brr [ ] = {7, 2, 5, 4, 6, 3, 5, 3} Output:Missing numbers: {4, 6} Problem Statement: Let me try to simplify the problem a little first. The problem description is quite verbose and we narrow down it to quite an extent. To summarize, the artist has an original array brr, and …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More