Skip to content

Instantly share code, notes, and snippets.

View SH4DY's full-sized avatar

Ramon SH4DY

View GitHub Profile
@SH4DY
SH4DY / 0_reuse_code.js
Last active August 29, 2015 14:16
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@SH4DY
SH4DY / LibDependencies.js
Last active August 29, 2015 14:17
Library dependencies - Graph topological sorting per DFS
//Topological sorting - Example of library dependencies
//Imagine you are given library dependencies in this form in a FILE:
// 4 libraries, 5 dependencies
// 0 -> 1
// 1 -> 2
// 2 -> 3
// 2 -> 4
// 3 ->
// 4 ->
// Meaning library 0 depends on lib 1...lib 3 depends on nothing etc
@SH4DY
SH4DY / rand5.js
Last active October 10, 2016 02:13
rand5() - Weekly interview question from interview cake
/*
You have a function rand7() that generates a random integer from 1 to 7.
Use it to write a function rand5() that generates a random integer from 1 to 5.
rand7() returns each integer with equal probability.
rand5() must also return each integer with equal probability.
*/
function rand5(){
var x = rand7();
while(x > 5){
@SH4DY
SH4DY / productExceptIndexDP.java
Last active October 10, 2016 02:13
/*You have an array of integers, and for each index you want to find the product of every integer except the integer at that index. Write a function get_products_of_all_ints_except_at_index() that takes an array of integers and returns an array of the products.*/ Greedy/DP approach
/*You have an array of integers, and for each index you want to find the product
of every integer except the integer at that index.
Write a function get_products_of_all_ints_except_at_index() that takes an array
of integers and returns an array of the products.*/
public static int[] get_products_of_all_ints_except_at_index_dp(int[] arr){
//build front
int[] front = new int[arr.length];
front[0] = 1;
for(int i = 1; i < arr.length; i++){
@SH4DY
SH4DY / parentheses.java
Created March 22, 2015 11:13
. A common problem for compilers and text editors is determining whether the parentheses in a string are balanced and properly nested. For example, the string ((())())() contains properly nested pairs of parentheses, which the strings )()( and ()) do not. Give an algorithm that returns true if a string contains properly nested and balanced paren…
//return -1: correct parentheses
//return n: Position of first offending bracket
public static int parantheses(char[] chrs){
Stack s = new Stack();
int i = 0;
for(;i<chrs.length;i++){ //Checks for erroneous chars omitted
char c = chrs[i];
@SH4DY
SH4DY / swap.java
Last active October 10, 2016 02:13
Swap two numbers in-place ==> without using a temp variable
//This method works only with integers
int a = 5;
int b = 7;
a = a-b; //diff. a = -2
b = a+b; // b = 5
a = b-a; // a = 7
//This method relies on bit manipulation and does not depend on the number format
int c = 3;
@SH4DY
SH4DY / permutationPalindrome.java
Created April 2, 2015 19:25
Give an algorithm to decide whether a given string or ANY of its permutations is a Palindrome. There is an O(n) algorithm for this problem!
/**
* Decide whether a given string or ANY
* of its permutations is a Palindrome.
*
* The given solution works in O(n). Building the parity map
* takes n steps, iterating through the map takes
* not more than n steps (size of the alphabet).
* Space consumption: Map with the size of the alphabet. (constant)
* @param str
* @return
@SH4DY
SH4DY / forkJoin.java
Last active October 10, 2016 02:12
Find the greatest number in an array with a multi-threaded fork/join approach
/**
* Find the greatest number in an array with a multi-threaded fork/join approach.
*
* This is a simple recursive divide&conquer approach to search through a list
* and return the max-element. It is using a subclass of java.util.concurrent.ForkJoinTask
* (namely RecursiveTask<V>).
*
* Theoretical aspect: Amdahl's Law.
* Possible speedup with n processors: S(n) = 1 / (B + 1-B*(1/n))
* where B is the fractal of the programm which can only be
@SH4DY
SH4DY / RomanConversion.java
Created April 8, 2015 20:44
Roman number conversion, following the rules: Symbols are placed from left to right in order of value, starting with the largest. However, in a few specific cases,[2] to avoid four characters being repeated in succession (such as IIII or XXXX) these can be reduced using subtractive notation as follows:[3][4] the numeral I can be placed before V …
public class RomanConversion {
public static void main(String[] args){
System.out.println(toRoman(1904));
}
public static String toRoman(int x){
StringBuilder sb = new StringBuilder();
String number = x + "";
@SH4DY
SH4DY / Inflight.java
Created May 12, 2015 07:39
In-Flight Entertainment. Knapsack with only 2 integer values. https://www.interviewcake.com/question/inflight-entertainment
//We can do this with only one loop
//thanks to the fact that we are looking for
//EXACTLY 2 movies which match the length.
//Duplicates are not being counted
//as we check for a hit FIRST and then put the
//value of the first movie into the map.
public static boolean isTwoMovies(int fl, int[] ml){
HashMap<Integer, Boolean> hm = new HashMap<Integer, Boolean >();