Skip to content

Instantly share code, notes, and snippets.

@artlovan
artlovan / CountNegativeIntegersInMatrix.java
Created June 6, 2017 21:40
CountNegativeIntegersInMatrix
import java.util.ArrayList;
import java.util.List;
public class CountNegativeIntegersInMatrix {
public static void main(String[] args) {
int[][] data = new int[][]{
{-3, -2, -1, 0, 1, 2, 4, 6, 7, 8},
{-3, -2, -1, 1},
{-2, 2, 3, 4},
{4, 5, 7, 8}
@artlovan
artlovan / TrafficLight.java
Created May 22, 2017 22:05
Simple TrafficLight Impl
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class TrafficLight {
public static void main(String[] args) throws InterruptedException {
Road north = new Road(Direction.NORTH, Color.RED);
Road south = new Road(Direction.SOUTH, Color.RED);
Road east = new Road(Direction.EAST, Color.RED);
Road west = new Road(Direction.WEST, Color.RED);
@artlovan
artlovan / PathsWithSum.java
Created May 21, 2017 20:30
PathsWithSum (_4_12)
import java.util.ArrayList;
import java.util.List;
/**
* Paths with Sum: You are given a binary tree in which each node contains
* an integer value (which might be positive or negative).
* Design an algorithm to count the number of paths that sum to a given value.
* The path does not need to start or end at the root or a leaf,
* but it must go downwards (traveling only from parent nodes to child nodes).
* <p>
@artlovan
artlovan / CheckSubtree.java
Created May 21, 2017 18:36
CheckSubtree (_4_10)
import java.util.ArrayList;
import java.util.List;
/**
* Check Subtree: T1 and T2 are two very large binary trees, with T1 much bigger than T2.
* Create an algorithm to determine if T2 is a subtree of T1.
* A tree T2 is a subtree of T1 if there exists a node n in T1 such that the subtree of n is identical to T2.
* That is, if you cut off the tree at node n, the two trees would be identical.
* <p>
* Hints: #4, #77, #78, #37, #37
@artlovan
artlovan / Successor.java
Created May 20, 2017 22:52
Successor (_4_6)
/**
* Successor: Write an algorithm to find the "next" node (i.e., in-order successor)
* of a given node in a binary search tree. You may assume that each node has a link to its parent.
* Hints: #79, #91
*/
public class Successor {
public static void main(String[] args) {
Node n6 = new Node(25, null, null, null);
Node n5 = new Node(13, null, null, null);
@artlovan
artlovan / ValidateBST.java
Created May 20, 2017 22:51
ValidateBST (_4_5)
/**
* Validate BST: Implement a function to check if a binary tree is a binary search tree.
* Hints: #35, #57, #86, # 773, # 728
*/
public class ValidateBST {
public static void main(String[] args) {
Node n6 = new Node(25, null, null);
Node n5 = new Node(13, null, null);
Node n4 = new Node(8, null, null);
@artlovan
artlovan / BuildOrder.java
Created May 20, 2017 22:37
BuildOrder (_4_7)
import java.util.*;
/**
* Build Order: You are given a list of projects and a list of dependencies
* (which is a list of pairs of projects, where the second project is dependent on the first project).
* All of a project'sdependencies must be built before the project is.
* Find a build order that will allow the projects to be built.
* If there is no valid build order, return an error.
* <p>
* EXAMPLE
@artlovan
artlovan / CheckBalanced.java
Last active May 20, 2017 16:37
CheckBalanced (_4_4)
/**
* Check Balanced: Implement a function to check if a binary tree is balanced.
* For the purposes of this question, a balanced tree is defined to be a tree such that
* the heights of the two subtrees of any node never differ by more than one.
* <p>
* Hints : # 27, # 33 , # 49 , # 705 , # 724
*/
public class CheckBalanced {
public static void main(String[] args) {
Node n10 = new Node(14, null, null);
@artlovan
artlovan / ListOfDepths.java
Created May 20, 2017 11:41
ListOfDepths (_4_3)
import java.util.ArrayList;
import java.util.LinkedList;
/**
* List of Depths: Given a binary tree, design an algorithm which creates
* a linked list of all the nodes at each depth (e.g., if you have a tree with depth 0, you'll have 0 linked lists).
* Hints: #107, #123, #735
*/
public class ListOfDepths {
public static void main(String[] args) {
@artlovan
artlovan / RouteBetweenNodes.java
Created May 20, 2017 11:40
RouteBetweenNodes (_4_1)
import java.util.LinkedList;
/**
* Route Between Nodes: Given a directed graph, design an algorithm to find out
* whether there is a route between two nodes.
* Hints: #127
*/
public class RouteBetweenNodes {
public static void main(String[] args) {
// Node node8 = new Node("node8", new Node[]{});