Skip to content

Instantly share code, notes, and snippets.

View nsivabalan's full-sized avatar

Sivabalan Narayanan nsivabalan

View GitHub Profile
@nsivabalan
nsivabalan / NoStarveReaderWriter
Created December 19, 2013 06:27
No Starvation Reader Writer
public class NoStarveReaderWriterLock {
private final Semaphore readMutex = new Semaphore(1);
private final Semaphore writeMutex = new Semaphore(1);
private final Semaphore noStarveMutex = new Semaphore(1);
private final Set<Long> readerIds = Collections
.synchronizedSet(new HashSet<Long>());
private volatile Long writerId;
public void readLock() {
try {
@nsivabalan
nsivabalan / TextJustification
Created December 18, 2013 17:11
Text Justification
public class TextJustification {
public static void justifyText(String[] words, int width)
{
if(words == null || words.length == 0) return ;
//checkForIllegalArgs(words, width)
int start = 0;
int end = 0;
int wordLen = words.length;
@nsivabalan
nsivabalan / TextJustification
Created December 18, 2013 16:52
Text Justification
public class TextJustification {
public static void justifyText(String[] words, int width)
{
if(words == null || words.length == 0) return ;
//checkForIllegalArgs(words, width) TO DO
int start = 0;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class RetrieveShuffledSentence {
static ArrayList<String> origDictWords ;
static Map<String, String> dictMap;
public static void populateDictionary(String[] dict)
{
package facebook.onlinequiz;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.StringTokenizer;
public class Solution {
int totalTests = 0;
@nsivabalan
nsivabalan / KdTrees
Last active December 27, 2015 21:09
import java.util.Comparator;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Queue;
/**
KDTrees. Used to find KNearestNeighbour among N Points in a KD Plane in O(logK) time.
Here is the implementation of 2D tress to find K nearest neighbours for any point among N given points.
Run time complexity : O((N+k)logK).
public class FindSetBits {
public static int getTotalSetBits(byte[] b, int offset, int endIndex)
{
int size = b.length;
length = endIndex - offset;
if(offset > size*8) throw new IllegalArgumentException("Offset overflow");
if((offset + length )> size*8) throw new IllegalArgumentException("Offset overflow");
int startIndex = offset;