Data Structures and Algorithms

JavaScript code challenges


  1. arrayReverse: Write a function called reverseArray which takes an array as an argument. Without utilizing any of the built-in methods available to your language, return an array with elements in reversed order.

  2. insertShiftArray: Write a function called insertShiftArray which takes in an array and the value to be added. Without utilizing any of the built-in methods available to your language, return an array with the new value added at the middle index.

  3. binarySearch: Write a function called BinarySearch which takes in 2 parameters: a sorted array and the search key. Without utilizing any of the built-in methods available to your language, return the index of the array’s element that is equal to the search key, or -1 if the element does not exist

  4. LinkedList: Create a Node class that has properties for the value stored in the Node, and a pointer to the next Node. Within your LinkedList class, include a head property. Upon instantiation, an empty Linked List should be created.

  • Define a method called insert which takes any value as an argument and adds a new node with that value to the head of the list with an O(1) Time performance.
  • Define a method called includes which takes any value as an argument and returns a boolean result depending on whether that value exists as a Node’s value somewhere within the list.
  • Define a method called toString (or str in Python) which takes in no arguments and returns a string representing all the values in the Linked List, formatted as: "{ a } -> { b } -> { c } -> NULL"
  • Write the following methods for the Linked List class: (1) append(value) which adds a new node with the given value to the end of the list (2) insertBefore(value, newVal) which add a new node with the given newValue immediately before the first value node (3) insertAfter(value, newVal) which add a new node with the given newValue immediately after the first value node
  • Write a method for the Linked List class which takes a number, k, as a parameter. Return the node’s value that is k from the end of the linked list. You have access to the Node class and all the properties on the Linked List class as well as the methods created in previous challenges.
  • Write a function called mergeLists which takes two linked lists as arguments. Zip the two linked lists together into one so that the nodes alternate between the two lists and return a reference to the head of the zipped list. Try and keep additional space down to O(1). You have access to the Node class and all the properties on the Linked List class as well as the methods created in previous challenges.
  1. Stacks and Queues: Create a brand new PseudoQueue class. Do not use an existing Queue. Instead, this PseudoQueue class will implement our standard queue interface (the two methods listed below), but will internally only utilize 2 Stack objects. The Stack instances have only push, pop, and peek methods. You should use your own Stack implementation. Instantiate these Stack objects in your PseudoQueue constructor. Ensure that you create your class with the following methods:
  • enqueue(value) which inserts value into the PseudoQueue, using a first-in, first-out approach.
  • dequeue() which extracts a value from the PseudoQueue, using a first-in, first-out approach.
  • Also, Create a class called AnimalShelter which holds only dogs and cats. The shelter operates using a first-in, first-out approach. Implement the following methods:
  • enqueue(animal): adds animal to the shelter. animal can be either a dog or a cat object.
  • dequeue(pref): returns either a dog or a cat. If pref is not "dog" or "cat" then return null.
  • Also, create a function called multiBracketValidation. Your function should take a string as its only argument, and should return a boolean representing whether or not the brackets in the string are balanced. There are 3 types of brackets:
  • Round Brackets : ()
  • Square Brackets : []
  • Curly Brackets : {}
  1. Tree: Create a Node class that has properties for the value stored in the node, the left child node, and the right child node.
  • Create a BinaryTree class: Define a method for each of the depth first traversals called preOrder, inOrder, and postOrder which returns an array of the values, ordered appropriately.
  • Create a BinarySearchTree class: Define a method named add that accepts a value, and adds a new node with that value in the correct location in the binary search tree. Define a method named contains that accepts a value, and returns a boolean indicating whether or not the value is in the tree at least once.
  1. fizzBuzzTree: Write a function called FizzBuzzTree which takes a tree as an argument.Without utilizing any of the built-in methods available to your language, determine whether or not the value of each node is divisible by 3, 5 or both. Create a new tree with the same structure as the original, but the values modified as follows:
  • If the value is divisible by 3, replace the value with “Fizz”
  • If the value is divisible by 5, replace the value with “Buzz”
  • If the value is divisible by 3 and 5, replace the value with “FizzBuzz”
  • If the value is not divisible by 3 or 5, simply turn the number into a String.
  • Return the new tree.
  1. Binary Tree: Write a breadth first traversal method which takes a Binary Tree as its unique input. Without utilizing any of the built-in methods available to your language, traverse the input tree using a Breadth-first approach, and return a list of the values in the tree in the order they were encountered.
  • Also, Write an instance method called find-maximum-value. Without utilizing any of the built-in methods available to your language, return the maximum value stored in the tree. You can assume that the values stored in the Binary Tree will be numeric.