Skip to content

Instantly share code, notes, and snippets.

Woon Ket Wong woonketwong

Block or report user

Report or block woonketwong

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@woonketwong
woonketwong / jugglingDBWithPostgres
Last active Dec 29, 2015
JugglingDB with Postgres
View jugglingDBWithPostgres
var q = require('q');
var Schema = require('jugglingdb').Schema;
var schema = new Schema('postgres', {
database: 'woonketwong',
username: 'woonketwong'
});
// The first argument to schema.define is the table
// and schema name. Do not use any capital letter
// in the table name because the database create table
@woonketwong
woonketwong / selectionRank
Last active Dec 31, 2015
Selection Rank is a well known algorithm in computer science to find the ith smallest (or largest) element in an array in linear time. If the elements are unique, you can find the ith smallest or largest element in expected O(n) time. The code below implements finding the ith smallest element in javascript.
View selectionRank
var selectionRank = function(array, rank){
// randomly select a pivot
var pivotIndex = Math.floor( (Math.random() * array.length) );
var pivot = array[pivotIndex];
// left array stores the smallest <rank> elements in the array
var leftArr = [];
var rightArr = [];
// partition into left and right arrays
for (var i = 0; i < array.length; i++){
@woonketwong
woonketwong / sortAndGroupAnagram
Created Dec 30, 2013
A method to sort an array of strings so that all the anagrams next to each other.
View sortAndGroupAnagram
var sortAndGroupAnagram = function(array){
var result = {};
var sortedResult = [];
var currentKey;
var allKey;
for (var i = 0; i < array.length; i++){
currentKey = array[i].split('').sort().join('');
if ( result.hasOwnProperty(currentKey) ){
result[currentKey].push(array[i]);
@woonketwong
woonketwong / mergeTwoSortedArray
Created Dec 31, 2013
Given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order.
View mergeTwoSortedArray
var mergeTwoSortedArray = function(array1, array2){
var array1IndexEnd = array1.length - array2.length - 1;
var arrayExtraIndexEnd = array1.length - 1;
var array2IndexEnd = array2.length - 1;
while( array2IndexEnd >= 0){
if ( array1[array1IndexEnd] > array2[array2IndexEnd]){
array1[arrayExtraIndexEnd] = array1[array1IndexEnd];
arrayExtraIndexEnd--;
array1IndexEnd--
@woonketwong
woonketwong / findLocInInterspersedSortedArr
Created Dec 31, 2013
A binary search function on a sorted array of strings which is interspersed with empty strings.
View findLocInInterspersedSortedArr
var findLocInInterspersedSortedArr = function(strArr, target, first, last){
var mid = Math.floor( last - first / 2);
if (strArr[mid] === ''){
var left = mid - 1;
var right = mid + 1;
while (true){
if (left < first && right > last){
return -1;
@woonketwong
woonketwong / quickSort
Created Jan 10, 2014
Quick sort implementation in JavaScript.
View quickSort
var quickSort = function(array, left, right){
var leftIndex = partition(array, left, right);
if (left < leftIndex - 1){
quickSort(array, left, leftIndex-1);
}
if (right > leftIndex){
quickSort(array, leftIndex, right);
@woonketwong
woonketwong / insertionSort
Created Jan 12, 2014
Insertion sort in javascript
View insertionSort
var insertionSort = function(array){
var target;
var sortedIndex = 0;
var targetIndex;
for(var i = 0; i < array.length; i++){
target = array[i];
targetIndex = i;
for(var j = i; j >= sortedIndex; j--){
@woonketwong
woonketwong / breathFirstSearch
Created Jan 12, 2014
Breath first search implementation with a queue.
View breathFirstSearch
var breathFirstSearch = function(node){
var queue = [];
// visit root
console.log("Node key, value", node.key, node.value);
root.visit=true;
queue.push(root);
while(queue.length !== 0){
currentNode = queue.pop();
@woonketwong
woonketwong / isBalanced
Last active Jan 3, 2016
Implement a function to check if a tree is balanced. For the purposes of this question, a balanced tree is defined to be a tree such that no two leaf nodes differ in distance from the root by more than one.
View isBalanced
var isBalanced = function(node){
var maxDepth = function(node){
if (node === undefined){
return 0;
}
return 1 + Math.max(maxDepth(node.next[0]), maxDepth(node.next[1]));
}
var minDepth = function(node){
@woonketwong
woonketwong / searchNodeUsingDFS
Created Jan 12, 2014
Given a directed graph, design an algorithm to find out whether there is a route between two nodes using DFS.
View searchNodeUsingDFS
var search = function(startNode, endNode){
//visit startNode
console.log("Visting:", startNode.key);
if (startNode.key === endNode.key){
return true;
}
startNode.visit = true;
You can’t perform that action at this time.