Skip to content

Instantly share code, notes, and snippets.

@wszdwp
wszdwp / gist:6633006
Last active December 23, 2015 12:09
Coursera_Algorithm class practice: Generic stack using linkedlist implementation
public class MyStack<Item>
{
private Node first = null;
private class Node
{
Item item;
Node next;
}
@wszdwp
wszdwp / gist:6633065
Last active December 23, 2015 12:09
Coursera_Algorithm class practice: Generic stack using fixed size array implementation
public class MyArrayStack<Item>
{
private Item[] s;
private int index;
public MyArrayStack(int capacity) {
s = (Item[]) new Object[capacity];
index = 0;
}
public boolean isEmpty() {
@wszdwp
wszdwp / gist:6633128
Last active December 23, 2015 12:09
Coursera_Algorithm class practice: Generic stack using variable size array implementation
public class MyVarSizeArrayStack<Item>
{
private Item[] s;
private int index;
public MyVarSizeArrayStack() {
s = (Item[]) new Object[1];
index = 0;
}
@wszdwp
wszdwp / gist:6663203
Created September 22, 2013 19:56
Cracking the interview Question19.5
public class Question195
{
public static class Result
{
private int hits;
private int pseudohits;
}
public static Result estimate(String guess, String solution) {
Result res = new Result();
@wszdwp
wszdwp / gist:6663212
Last active December 23, 2015 16:39
Cracking the interview 19.4
public class Question194
{
public static int getMax(int a, int b) {
int c = a - b;
int k = ( c >> 31) & 0x1;
return a - k * c;
}
public static void main(String[] args) {
int a = 5;
@wszdwp
wszdwp / gist:6663220
Created September 22, 2013 19:58
Cracking the interivew 19.2
public class Question192
{
public static int numZeros(int num) {
int count = 0;
if (num < 0) {
return -1;
}
for(int i = 5; num / i > 0; i *= 5) {
count += num / i;
}
@wszdwp
wszdwp / gist:6663231
Created September 22, 2013 19:59
Cracking the interivew 19.1
//Java is not working ??? pass by value??
public class Question191
{
public static void swap(int a, int b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
public static void main(String[] args) {
@wszdwp
wszdwp / gist:6663459
Last active December 23, 2015 16:39
Crack the interview 19.7
public class Question197
{
public static int maxsum(int[] a) {
int maxsum = 0;
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
if( maxsum < sum) {
maxsum = sum;
} else if (sum < 0) {
@wszdwp
wszdwp / gist:6664869
Created September 22, 2013 23:29
Cracking the interview 19.8
import java.util.Hashtable;
public class Question198
{
public static Hashtable<String, Integer> createHashtable(String[] book) {
Hashtable<String, Integer> table = new Hashtable<String, Integer>();
for(int i = 0; i < book.length; i++) {
book[i] = book[i].toLowerCase();
if(book[i].trim() != "") {
if( !table.containsKey(book[i])) {
@wszdwp
wszdwp / gist:6665278
Created September 23, 2013 00:36
Cracking the interview 19.11
import java.util.Hashtable;
import java.util.Arrays;
public class Question1911
{
//hashtable implementation, needs to figure out the repeated printout pair and collision
public static void findpair(int sum, int[] a) {
Hashtable<Integer, Integer> table = new Hashtable<Integer, Integer>();
for(int i = 0; i < a.length; i++) {
if( !table.containsKey(a[i])) {