Skip to content

Instantly share code, notes, and snippets.

@woonketwong
woonketwong / jugglingDBWithPostgres
Last active December 29, 2015 05:09
JugglingDB with Postgres
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 October 3, 2023 13:18
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.
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 December 30, 2013 18:33
A method to sort an array of strings so that all the anagrams next to each other.
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 December 31, 2013 18:58
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.
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 December 31, 2013 20:07
A binary search function on a sorted array of strings which is interspersed with empty strings.
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 January 10, 2014 19:40
Quick sort implementation in JavaScript.
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 January 12, 2014 06:12
Insertion sort in javascript
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 January 12, 2014 18:01
Breath first search implementation with a queue.
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 January 3, 2016 01:19
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.
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 January 12, 2014 19:09
Given a directed graph, design an algorithm to find out whether there is a route between two nodes using DFS.
var search = function(startNode, endNode){
//visit startNode
console.log("Visting:", startNode.key);
if (startNode.key === endNode.key){
return true;
}
startNode.visit = true;