Hackerrank Day 18: Queues and Stacks

Task:

A 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?

screen-shot-2016-12-25-at-3-35-09-pm

My Solution: 

Screen Shot 2016-12-25 at 3.46.51 PM.png

screen-shot-2016-12-25-at-3-44-57-pm

Things learned in this challenge:

  • The attr_accessor method lets you create, read(attr_reader), and write (attr_writer) instance variables all in one step.   If we didn’t use it, we would have to write all of this out:

screen-shot-2016-12-25-at-3-52-37-pm

  • An array can be a stack or queue by limiting yourself to stack or queue methods (push, pop, shift, unshift). Using push / pop gives LIFO behavior (stack), while using push / shift gives FIFO behavior (queue).
  • A stack is a data structure that uses a principle called Last-In-First-Out (LIFO), meaning that the last object added to the stack must be the first object removed from it.At minimum, any stack should be able to perform the following three operations:
    • Peek: Return the object at the top of the stack (without removing it).
    • Push: Add an object passed as an argument to the top of the stack.
    • Pop: Remove the object at the top of the stack and return it.
  • A queue is a data structure that uses a principle called First-In-First-Out (FIFO), meaning that the first object added to the queue must be the first object removed from it. You can analogize this to a checkout line at a store where the line only moves forward when the person at the head of it has been helped, and each person in the line is directly behind the person whose arrival immediately preceded theirs.At minimum, any queue should be able to perform the following two operations:
    • Enqueue: Add an object to the back of the line.
    • Dequeue: Remove the object at the head of the line and return it; the element that was previously second in line is now at the head of the line.
Advertisements
Hackerrank Day 18: Queues and Stacks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s