Skip to content

Instantly share code, notes, and snippets.

@artlovan
artlovan / MinimalTree.java
Created May 19, 2017 02:23
MinimalTree (_4_2)
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
Minimal Tree: Given a sorted (increasing order) array with unique integer elements,
write an algorithm to create a binary search tree with minimal height.
Hints: #19, #73, #176
*/
public class MinimalTree {
@artlovan
artlovan / ThreeInOne.java
Created May 18, 2017 23:37
ThreeInOne (_3_1)
import java.util.EmptyStackException;
/**
* Three in One: Describe how you could use a single array to implement three stacks.
* Hints: #2, #72, #38, #58
*/
public class ThreeInOne {
public static void main(String[] args) {
int[] data = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
MyStack<Integer> st = new MyStack<>();
@artlovan
artlovan / AnimalShelter.java
Created May 18, 2017 01:30
AnimalShelter (_3_6)
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
/**
* Animal Shelter: An animal shelter, which holds only dogs and cats,
* operates on a strictly "first in, first out" basis.
* People must adopt either the "oldest" (based on arrival time) of all animals at the shelter,
* or they can select whether they would prefer a dog or a cat (and will receive the oldest animal of that type).
* They cannot select which specific animal they would like. Create the data structures to maintain
@artlovan
artlovan / SortedStack.java
Created May 18, 2017 00:26
SortedStack (_3_5)
import java.util.EmptyStackException;
/**
* Sort Stack: Write a program to sort a stack such that the smallest items are on the top.
* You can use an additional temporary stack, but you may not copy the elements into any
* other data structure (such as an array).
* The stack supports the following operations: push, pop, peek, and isEmpty.
* Hints: # 75, #32, #43
*/
public class SortStack {
@artlovan
artlovan / QueueViaStacks.java
Created May 17, 2017 23:57
QueueViaStacks (_3_4)
import java.util.EmptyStackException;
/**
* Queue via Stacks: Implement a MyQueue class which implements a queue using two stacks.
* Hints: #98, #7 74
*/
public class QueueViaStacks {
public static void main(String[] args) {
MyQueue<Integer> myQueue = new MyQueue<>();
myQueue.add(2);
@artlovan
artlovan / StackOfPlates.java
Created May 17, 2017 23:32
StackOfPlates (_3_3)
/**
* Stack of Plates: Imagine a (literal) stack of plates. If the stack gets too high, it might topple.
* Therefore, in real life, we would likely start a new stack when the previous stack exceeds some threshold.
* Implement a data structure SetOfStacks that mimics this. SetOfStacks should be composed of
* several stacks and should create a new stack once the previous one exceeds capacity.
* SetOfStacks.push() and SetOfStacks.pop() should behave identically to a single stack (that is,
* pop() should return the same values as it would if there were just a single stack).
* <p>
* FOLLOW UP
* Implement a function popAt(int index) which performs a pop operation on a specific sub-stack.
@artlovan
artlovan / StackMin.java
Created May 17, 2017 21:49
StackMin (_3_2)
/**
* Stack Min: How would you design a stack which, in addition to push and pop,
* has a function min which returns the minimum element?
* Push, pop and min should all operate in 0(1) time.
* Hints: #27, #59, #78
*/
public class StackMin {
public static void main(String[] args) {
MyStack<Integer> stack = new MyStack<>();
stack.push(2);
@artlovan
artlovan / LoopDetection.java
Created May 17, 2017 00:46
LoopDetection (_2_8)
import java.util.ArrayList;
import java.util.List;
/**
* Loop Detection: Given a circular linked list, implement an algorithm that
* returns the node at the beginning of the loop.
* DEFINITION
* Circular linked list: A (corrupt) linked list in which a node's next pointer points
* to an earlier node, so as to make a loop in the linked list.
* <p>
@artlovan
artlovan / Palindrome.java
Created May 17, 2017 00:00
Palindrome (_2_6)
/**
* Palindrome: Implement a function to check if a linked list is a palindrome.
* Hints: #5, #13, #29, #61, #101
*/
public class Palindrome {
public static void main(String[] args) {
Node<Integer> node1 = new Node<>(2, null);
Node<Integer> node2 = new Node<>(9, node1);
Node<Integer> node3 = new Node<>(2, node2);
@artlovan
artlovan / SumLists.java
Created May 16, 2017 23:37
SumLists (_2_5)
import java.util.ArrayList;
import java.util.List;
/**
* Sum Lists: You have two numbers represented by a linked list, where each node contains a single digit.
* The digits are stored in reverse order, such that the 1's digit is at the head of the list.
* Write a function that adds the two numbers and returns the sum as a linked list.
* <p>
* EXAMPLE
* Input: (7-> 1 -> 6) + (5 -> 9 -> 2).That is, 617 + 295.