learn kro favicon img

Day 18: Queues and Stacks | HackerRank Solution

Hello coders, today we are going to solve Day 18: Queues and Stacks HackerRank Solution in C++, Java and Python.

Task

Welcome to Day 18! Today we’re learning about Stacks and Queues

palindrome is a word, phrase, number, or other sequence of characters which reads the same backwards and forwards. Can you determine if a given string, s, is a palindrome?

To solve this challenge, we must first take each character in senqueue it in a queue, and also push that same character onto a stack. Once that’s done, we must dequeue the first character from the queue and pop the top character off the stack, then compare the two characters to see if they are the same; as long as the characters match, we continue dequeueing, popping, and comparing each character until our containers are empty (a non-match means s isn’t a palindrome).

Write the following declarations and implementations:

  1. Two instance variables: one for your stack, and one for your queue.
  2. void pushCharacter(char ch) method that pushes a character onto a stack.
  3. void enqueueCharacter(char ch) method that enqueues a character in the queue instance variable.
  4. char popCharacter() method that pops and returns the character at the top of the stack instance variable.
  5. char dequeueCharacter() method that dequeues and returns the first character in the queue instance variable.

Input Format

You do not need to read anything from stdin. The locked stub code in your editor reads a single line containing string s. It then calls the methods specified above to pass each character to your instance variables.

Constraints

  • s is composed of lowercase English letters.

Output Format

You are not responsible for printing any output to stdout.
If your code is correctly written and s is a palindrome, the locked stub code will print The word, s, is a palindrome; otherwise, it will print The word, s, is not a palindrome.

Sample Input

Sample Output

Solution – Day 18: Queues and Stacks

C++

Java

Python

Disclaimer: The above Problem (Day 18: Queues and Stacks) is generated by Hacker Rank but the Solution in Provided by Learnkro. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *