This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |