Skip to content

Instantly share code, notes, and snippets.

View nsivabalan's full-sized avatar

Sivabalan Narayanan nsivabalan

View GitHub Profile
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;
@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).
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;
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)
{
@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;
@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 / 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 / Lucky Strings
Created December 28, 2013 19:00
Lucky Strings : Lucky numbers are those numbers which contain only "4" and/or "5". For example 4, 5, 44, 54,55,444 are lucky numbers while 457, 987 ,154 are not. Lucky number sequence is one in which all lucky numbers exist in increasing order for example 4,5,44,45,54,55,444,445,454,455... Now we concatenate all the lucky numbers (in ascending o…
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
public class LuckyStrings {
public static void main(String args[]) throws NumberFormatException, IOException
{
@nsivabalan
nsivabalan / Scrubber.java
Created October 13, 2016 06:19
Interface for Scrubber
/**
* Class used for scrubbing the log for any inconsistencies
*/
public class Scrubber {
/**
* Used to start scrubbing the log for consistency checks
*/
public void start();
import com.github.ambry.clustermap.PartitionId;
import com.github.ambry.config.StoreConfig;
import com.github.ambry.store.BlobStore;
import com.github.ambry.utils.Time;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;