data-structures-and-algorithms-java

Stack & Queues

Stack: A stack is a data structure that consists of Nodes. Each Node references the next Node in the stack, but does not reference its previous.

Stacks follow these concepts:

Queue
Queues follow these concepts:

Challenge

write functions for the Stack class:

and function for the Queue class:

Approach & Efficiency

Time complexity for all the functions is :O(1)
Space complexity: O(1)

API

Challenge Summary

have a queue made of 2 stacks and perform enqueue and dequeue in a FIFO order.

Whiteboard Process

whiteboard

Approach & Efficiency

complexity:
enqueue : O(n)
dequeue:O(1)

Solution

Verification: queue: [10]->[15]->[20]
enqueue input: 5
output: [5]->[10]->[15]->[20]

dequeue input: none
dequeue output: 20 [5]->[10]->[15]

Challenge Summary

Create a class called AnimalShelter which holds only dogs and cats, The shelter operates FIFO and has two methods
enqueue(animal)
  input: an object
  no output
dequeue(String)
  input : String
  output: object

Whiteboard Process

whiteboard

Approach & Efficiency

complexity:
time & space = O(1) for both methods

Solution

Challenge Summary

create a function that checks whether the brackets are balanced
input: String
output: boolean

Whiteboard Process

whiteboard

Approach & Efficiency

complexity:
time & space complexity: O(n)

Solution

Verification:

input : “()[[Extra Characters]]”
output: TRUE

input:”[({}]”
output: false