Saturday, November 12, 2016

Given an input file with four billion integers, provide an algorithm to generate an integer which is not contained in the file.

//Given an input file with four billion integers, provide an algorithm to generate an integer which is not contained in the file. Assume you have 1 GB memory. Follow up with what you would do if you have only 10 MB of memory.
//reference source:
//in that page MEMORY LIMITS SECTION
//below is c++solution
//Note : please create a file with name appropriately or download one from https://github.com/careercup/CtCI-6th-Edition/tree/master/Java/Ch%2010.%20Sorting%20and%20Searching/Q10_07_Missing_Int


PARTA:



#include <iostream>
#include <bitset>
#include <fstream>
#include <iomanip>
#define BITSPERWORD 32
#define SHIFT 5
#define MASK 0X1F
#define N 2147483647

using namespace std;

void setBit(int i, int *a)
{
    a[i>>SHIFT] |= (1<<(i&MASK));
}

void clearBit(int i,int *a)
{
    a[i>>SHIFT] &= ~(1<<(i&MASK));
}

int testBit(int i,int *a)
{
    return a[i>>SHIFT] & (1<<(i&MASK));
}

void findNumber(){
    int a[1+N/BITSPERWORD];
   
    const char* filename = "input1.txt";
    ifstream inFile(filename,std::ios::in);
    
    // Make sure the file stream is good
    if(!inFile) {
        cout << endl << "Failed to open file " << filename;
        
    }
    
    int n = 0;
    while(inFile)
    {
        inFile>>n;
        clearBit(n,a);
        setBit(n,a);
        
    }
    for(int i=0;i<N;i++)
    {
        if(!testBit(i, a))
        {
            cout<<i;
        }
    }
}


int main(int argc, const char * argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    findNumber();
    return 0;

}



partB Follow UP:






#include <iostream>
#include <bitset>
#include <fstream>
#include <iomanip>
#define BITSPERWORD 32
#define SHIFT 5
#define MASK 0X1F
#define N 2147483647
#define RANGESIZE 100

/* 1 << (i & MASK): left shift from 0 to 31, represents 2^i
 i >> SHIFT: array index
 0-31   a[0]
 32-63  a[1]
 64-95  a[2]
 ...    ...
 */

//set rangeSIZE as 1048576 which == 2power20 if the array can hols size of 10MB which == 2power 20
//#define RANGESIZE 1048576

using namespace std;


void setBit(int i, int *a)
{
    a[i>>SHIFT] |= (1<<(i&MASK));
}

//below function will clear the bit as below
//for eg if we have
//i 32 a is the bitvector

//i>>SHIFT will shift the i from 32 to 1 so we are accessing index =1 in the array a
//i&MASK will transform MASK  from 31 to 23

//1<<(i&MASK) will left shift 1 and make 23 bit position as 1
//hence a[i>>SHIFT] & (1<<(i&MASK)) which check if 23rd position is 0 or 1 which indictes the presence of the number throught bit position

void clearBit(int i,int *a)
{
    a[i>>SHIFT] &= ~(1<<(i&MASK));
}

int testBit(int i,int *a)
{
    return a[i>>SHIFT] & (1<<(i&MASK));
}
bool checkFile(ifstream *inFile)
{
    if(!(*inFile))
    {
        
        return false;
    }
    return true;
}
void getCountPerBlock(const char* filename,int *blocks)
{
    ifstream inFile(filename,std::ios::in);
   
        if(checkFile(&inFile)==true)
        {
            int n = 0;
            while(!inFile.eof())
            {
                inFile>>n;
                blocks[n/RANGESIZE]++;
                
            }
        }
        else
        {
            cout << endl << "Failed to open file ";
        }
    
}
int getMissingIndex(int *blocks,int size)
{
    for(int i=0;i<size;i++)
    {
        if(blocks[i]<RANGESIZE)
        {
            return i;
        }
    }
    return -1;
}
void setBitVector(int missingIndex,const char* fileName,int *blocks,int size,int *bitVector,int bitVectorSize)
{
    int startRange = RANGESIZE*missingIndex;
    int endRange   = RANGESIZE*missingIndex+RANGESIZE-1;
    int offSet = 0;
    ifstream inFile(fileName,std::ios::in);
    if(checkFile(&inFile)==true)
    {
        int n = 0;
        while(!inFile.eof())
        {
            inFile>>n;
            if(n>=startRange && n<=endRange)
            {
                offSet = n - (missingIndex*RANGESIZE);
                clearBit(offSet,bitVector);
                setBit(offSet,bitVector);
            }
        }
    }
    else
    {
        cout << endl << "Failed to open file ";
    }
}
int findZeroBit(int *bitArray,int size)
{
    
    for(int i=0;i<RANGESIZE;i++)
    {
        if(!testBit(i,bitArray))
        {
            return i;
        }
    }
    return -1;
}
void disp(int *blocks,int size)
{
    for(int i=0;i<size;i++)
    {
        cout<<blocks[i];
    }
}
void setArray(int *blocks,int size)
{
    for(int i=0;i<size;i++)
    {
        blocks[i]=0 ;
    }
}
void findNumber(){
    
    //change this value to INT_MAX when we test for a file which contains 1GB of consecutive numbers
    int maxInt = 500;
    //if the RAM size is 10 MB or the array size is of 2power20 == 10 MB set the rangesize as below
    
    
    
    
    const char* filename = "input2.txt";
    int blocks[maxInt/RANGESIZE+1];
    int bitVector[RANGESIZE/BITSPERWORD+1];
    int bitVectorSize = (int)(sizeof(blocks)/sizeof(blocks[0]));
    int size = (int)(sizeof(blocks)/sizeof(blocks[0]));
    
    setArray(blocks,size);
    
    getCountPerBlock(filename,blocks);
   
    
    int missingIndex = getMissingIndex(blocks,size);
    
    if(missingIndex!=-1)
    {
        cout<<missingIndex;
        
        setBitVector(missingIndex,filename,blocks,size,bitVector,bitVectorSize);
        
        int offset = findZeroBit(bitVector,bitVectorSize);
        
        if(offset!=-1)
        {
            int missingValue = RANGESIZE*missingIndex +offset;
            cout<<"missig value is"<<missingValue;
        }
    }
    else{
        cout<<"There is no missing value";
    }
    

}


int main(int argc, const char * argv[]) {
    findNumber();
    return 0;
}

Saturday, November 5, 2016

Design Pattern Implementations Builder Pattern --------- Coffee Builder



Reference Website

Below tutorial  has very clearly explained the implementation of Builder Design Pattern

http://ramj2ee.blogspot.in/2013/12/builder-design-pattern-implementation.html#.WB4gMGVeDR0




package coffee_builder;
import java.util.*;
public class customer {

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc1 = new Scanner(System.in);
System.out.println("Please enter Drink Name");
String input = sc1.next();
Director.takeOrder(input);
}


}

package coffee_builder;
//Also called as Waiter
public class Director {

public static Beverage takeOrder(String beverageType)
{
BeverageBuilder bb = null;
if(beverageType.equalsIgnoreCase("Tea"))
{
bb = new TeaBuilder();
}
else if(beverageType.equalsIgnoreCase("Coffee"))
{
bb = new CoffeeBuilder();
}
else
{
System.out.println("Sorry we cannot take order other than      Coffee and Tea");
}
return bb.buildBeverage();
}

}

package coffee_builder;

public class TeaBuilder extends BeverageBuilder{

@Override
void setBeverageType() {
// TODO Auto-generated method stub
System.out.println("Preparing Tea");
getBeverage().setBeverageName("Tea");
}

@Override
void setWater() {
// TODO Auto-generated method stub
System.out.println("Adding 15 ml of water");
getBeverage().setWater(15);
}

@Override
void setMilk() {
// TODO Auto-generated method stub
System.out.println("Adding 30 ml of milk");
getBeverage().setMilk(30);
}

@Override
void setSugar() {
// TODO Auto-generated method stub
System.out.println("Adding 50 ml of sugar");
getBeverage().setSugar(50);
}

@Override
void setPowderQuantity() {
// TODO Auto-generated method stub
System.out.println("Adding 3 table spoons  of tea Powder");
getBeverage().setPowderQuantity(3);
}

@Override
Beverage createBeverage() {
// TODO Auto-generated method stub
return new Beverage();
}

}

package coffee_builder;

public class CoffeeBuilder extends BeverageBuilder{

@Override
void setBeverageType() {
// TODO Auto-generated method stub
System.out.println("Preparing Coffee");
getBeverage().setBeverageName("Coffee");
}

@Override
void setWater() {
// TODO Auto-generated method stub
System.out.println("Adding 15 ml of water");
getBeverage().setWater(15);
}

@Override
void setMilk() {
// TODO Auto-generated method stub
System.out.println("Adding 30 ml of milk");
getBeverage().setMilk(30);
}

@Override
void setSugar() {
// TODO Auto-generated method stub
System.out.println("Adding 50 ml of milk");
getBeverage().setSugar(50);
}

@Override
void setPowderQuantity() {
// TODO Auto-generated method stub
System.out.println("Adding 2 tbps of coffe powder");
getBeverage().setPowderQuantity(2);
}

@Override
Beverage createBeverage() {
// TODO Auto-generated method stub
return new Beverage();
}

}


package coffee_builder;

public abstract class BeverageBuilder {
private Beverage beverage;

public Beverage getBeverage() {
return beverage;
}

public void setBeverage(Beverage beverage) {
this.beverage = beverage;
}
public final Beverage buildBeverage()
{
beverage = createBeverage();
setBeverage(beverage);
setBeverageType();
setWater();
setMilk();
setSugar();
setPowderQuantity();
System.out.println(beverage.getBeverageName() + "Delivered");
return beverage;
}

abstract void setBeverageType();
abstract void setWater();
abstract void setMilk();
abstract void setSugar();
abstract void setPowderQuantity();
abstract Beverage createBeverage();
 
}


package coffee_builder;

public class Beverage {
private int water;
private int sugar;
private int milk;
private int powderQuantity;
private String beverageName;
public int getWater() {
return water;
}
public void setWater(int water) {
this.water = water;
}
public int getSugar() {
return sugar;
}
public void setSugar(int sugar) {
this.sugar = sugar;
}
public int getPowderQuantity() {
return powderQuantity;
}
public void setPowderQuantity(int powderQuantity) {
this.powderQuantity = powderQuantity;
}
public String getBeverageName() {
return beverageName;
}
public void setBeverageName(String beverageName) {
this.beverageName = beverageName;
}
public int getMilk() {
return milk;
}
public void setMilk(int milk) {
this.milk = milk;
}