Sunday, October 15, 2017

Cursor Jumps while Typing using Keyboard in Ubuntu 16.04 Dell Laptop.

Solution: 
Firstly decrease the speed of mouse pointer, between slow to average.

After that analyzed the Issue and the outcome is as follows: 

Looks like the following link helped. The issue is when we type the cursor jumps where the mouse pointer is.
This means the Issue the when we are typing, our touch pad (for mouse pointer) should be disabled.
The links which helped me are : 
Open the terminal and type the command: 
syndaemon -i 0.5 -d

This worked for me, my machine is Ubuntu 16.04 LTS.
If above doesn't works out then we might need to install the touchpad gui widget(touch pad indicator) and disable touchpad while typing. 
This can be found in the URL:http://ubuntuhandbook.org/index.php/2016/11/install-touchpad-indicator-in-ubuntu-16-04/

Sunday, May 3, 2015

REDIS : Single Threaded Model, Race Condition.

Lets assume we have multithreaded DB server, which can accept multiple connections and can execute multiple queries from different clients.
    Now let say Client 1 Issues a command : Incr X;
    Client 2 also Issues a Command : Incr X;
    Now If two Clients perform it at a same time then two possibilities can happen :
         If your DataBase server is on MultiCore CPU then then both threads can run parallel y.
     --If your DB server is with single Core then both can run concurrently.
     --In both cases : If queries are not performed atomically it can lead to Race Condition.

Since both threads trying to update the X, at almost same time(and due to this can cause inconsistent value of X).

        One more point to note : is INCR query requires to steps : a)Read the Value X b)Increment value X.
    For a small moment of time lets assume our Underlying Storage Engine of the DataBase provides exclusive Locks on Row/Table
        for update operation. Therefore it is safe to say that any of the two clients who got the Lock first, perform its operation, and release the lock
Hence there will be no race condition, as such.
In case you have multiple queries needs to executes as one Single atom, then we go by transaction and make it happen successfully.

NOTE : Lets say Client wants to execute the following command :
INCR X;
Now Increment X deals with two more granular operations : get X, update X = X+1;
Considering following scenario's(in case of multithreaded DB Server) :
May be my underlying Storage Engine behaves on occurrence of Update Query :
 -->It provides Exclusive DB Level Lock
 -->It provides  Exclusive Table Level Lock.
 -->It provides Exclusive Row Level Lock.
 -->It provides granular locks (on the basis of queries) {In that case we need to to execute INCR X in a Transaction}.
In any of the case Client needs to content for the Lock, grab the Lock, execute its query, release the Lock.

Therefore whatever be the case in Multithreaded DB Servers we need to make sure that only one Update Query executes at a time(If multiple clients wants to
update on same resource).{and this can be achieved by Locks, which is quite Obvious}.


NOTE : In case of Single Threaded Environment {DB Server is Single Threaded}, which means only one Thread will be using the Database at a time.
  So lets say we have two Clients scenario again : they both want to perform "get X" operation.
  Then the following will happen :
  Client #1 got the access of DB, send the command to REDIS, get the result ...Happy.
  Client #2 got the access of DB, send the command to REDIS, get the result ...Happy.
  It seems like get X operation is executed atomically, no interference.  Job Done.
 
  Now Lets say : Clients wants to Execute INCR X command :
  again, as per Single Threaded model , it will be executed atomically.
  Client #1 got the access of DB, send the command to REDIS, update X.
  Client #2 got the access of DB, send the command to REDIS, update X.
  Again no Interference, since it is Single Server Model.
 
  What happens, If Clients perform the following :
get X;
//DO SOME TASK
Incr X.

Now here as per our previous scenario's : get X {executed without interference}, But it might happen that other Client update the X value,
and when our old Client get the chance, it will Increment X with old value and not the newest value.
Hence Race Conditions occurs, and I think it is obvious, because If you want to execute multiple commands at once, why not go for Transactions.
Therefore WATCH/MULTI/EXEC comes into picture.
Now If we have used transaction{Then all our commands i.e get X and update X will run as Single Unit}.
EXEC : Will execute them one after other and WATCH : will keep track if any updates happened before executing the commands, If so retry the whole process again.

It seems like it is Responsive approach: Since no User/Client will get blocked{due to Locking} , which happened in case of Traditional RDBMS.
The only thing is that retries will happen on case of updations by other Clients.

But again above process have one severe drawback :
In case of Heavy Load : Number of retries will increase, because at the moment when you are going to execute your Commands Chain, some other Client update the
value, then your Exec will fail and you try the whole process again.
Now to avoid such scenario's under Heavy Load, Locks comes into Picture. It is same like Traditional Locks(you can fine grain your locks based on the application
requirements) and you lock certain Data-structure, perform the queries and release the Lock.

Summary :
In short Redis commands are atomic on its own, and given that Redis is Single Threaded it is quite safe to execute single command without worrying about Race Conditions.
But when you are dealing with multiple commands to be executed atomically then you need to consider Transactions/Locking etc.
In contrast : Some Storage Engines{which supports multiple requests} might give atomicity only on {finest granularity} commands for Example INCR X {comprise of GET X, UPDATE X = X+1}, in that
case RACE Conditions can occur, in such scenario's you need to put even INCR X in a transaction or LOCK the resource.

Wednesday, February 25, 2015

Java Multi Threading : Wait/ Notify/ NotifyAll

Lets say bunch of Readers wants to read updated Value of certain Resource, which is going to be updated by Writer. Then how Reader knows that Resource Fields have been updated by Writer.

Therefore to Synchronize such cases between Readers and Writers on common resource, three final methods of Object class have been used.
  1. wait()
  2. notify()
  3. notifyAll()
Wait : Readers wants to read updated value of resource, they register with resource object i.e when update happens on same object and when Writer notify it, readers will try to acquire lock on resource and read the updated resource.
  • Wait only being invoked when Reader have Lock Object, here in our case it is resource.
  • Once wait method is called, Reader releases the Lock Object.
  •  Now Only for the same registered Object(resource) Reader will get notification signals.
  • If Reader invokes wait on Object, which is different from Object Writer used for sending notification, Reader will never gets the notification signal.
  • Once Reader(s) are notified, now Reader(s) will try to content for the Lock(one of them gets the lock) read the updated value of resource. Similarly other Readers also gets turn to acquire lock and read the updated value.
  • Once Reader read the updated value, perform there Business Logic and come out from Synchronized Block, Reader will release the lock so that other Readers can acquire it. 
Notify: Writer enters the Synchronized Block, after acquiring lock performs his Business Logic, 
update the resource Object, once resource Object is updated it will notify the waiting threads(Readers) whom are waiting on same Lock.
  • Notify signals only one waiting Thread, which is decided by underlying Java Thread Manager
  • Once Writer signals notify(), then it doesn't means Reader rushed immediately to read the updates values. Firstly writer must release the Lock, which it will do once it comes out of Synchronized block.Once Lock is being released and waiting threads are notified, then [In case of notify()] notified Thread will acquire the Lock[Released by Writer] and then enter the Synchronized Block and completes from where he left[i.e statements after wait()].
Notify-All: In notifyAll, all threads who are registered with resource lock, will get the notifications.
  • Once notifyAll() is triggered, all threads waiting on same lock will get the signal and are ready in contention to acquire the lock.
  • Once Writer finishes its Job and release the Lock, Any one Reader will acquire the lock[which Thread, again decided by underlying Java Thread Manager Implementation].
  • Once the Reader gets the Lock, it will enter in Synchronized Block, where he left[i.e after wait() method] performs it tasks and on completing the Synchronized Block releases the Lock. 
  • Now Other remaining Threads will try to acquire the Lock, anyone of them will get it, enters synchronized block, completes its task and then release the Lock.
  • This process will keep on till all Registered Readers complete there Job.
   ----------------------------------------------------------------------------------------------------------------
Now we will see the Code for it. Also we will discuss the Code also. :

Basic Overview Of code : It consists of Three classes 
  1. Resource Class : On which Lock will be acquired and wait() and notify(), notifyAll() will be invoked.
  2. ReaderTask : Implements Runnable interface, implies readers jobs, wants to read updated Value of resource object.
  3. WriterTask : Implements Runnable interface, implies writer jobs, will update the resource object and notify registered waiting threads.
  4. Demo Class: which will create Let say 3 Readers and 1 Writer Thread, Bind Respective Tasks to them and start the threads.
-----------------------------------------------------------------------------------------------------------
Resource.java

   public class Resource {
  private String mesg;
  
          public void setMesg(String mesg){
 this.mesg =mesg;
  }
  public String getMesg(){
 return this.mesg;
  }
    }

------------------------------------------------------------------------------------------------
WaitThreadTask.java

public class WaitThreadTask implements Runnable {

private Resource resource;
public WaitThreadTask(Resource resource){
this.resource = resource;
}
@Override
public void run() {
// TODO Auto-generated method stub
synchronized(resource){
System.out.println("Before Reading Updated Value By : " +Thread.currentThread().getName() );
//We need to Take care to get the updated value, so waiting for writer thread to update value.
try {
//Release resource Lock & wait till any notification from Writer.
resource.wait();
System.out.println("Waiting is Over For : "+ Thread.currentThread().getName());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Read Updated Value
System.out.println("Updated Value of Resource Mesg :" + resource.getMesg() + " Read By :" +Thread.currentThread().getName());
}
}
}

-----------------------------------------------------------------------------------------------------------------
WriterThreadTask.java


public class WriterThreadTask implements Runnable{

private Resource resource;
public WriterThreadTask(Resource resource){
this.resource = resource;
}
@Override
public void run() {
// TODO Auto-generated method stub
synchronized(resource){
System.out.println("Before Updating Resource By : " + Thread.currentThread().getName());
//Updating resource Object Message
resource.setMesg("Hi How are You !!!");
resource.notify();
//resource.notifyAll();
//Once Writer Comes Out from Synch Block, Readers will Content to read the values.
System.out.println("Task Done By Writer Thread.");
}
}
}

----------------------------------------------------------------------------------------------------------------------

ThreadDemo.java

public class ThreadDemo {
public static void main(String args[]){
//Create Single Resource Object, which can act as Lock on Writer and Readers.
Resource lock = new Resource();
//Three Readers and One Writer runnable Tasks.
Runnable taskR1 = new WaitThreadTask(lock);
Runnable taskR2 = new WaitThreadTask(lock);
Runnable taskR3 = new WaitThreadTask(lock);
Runnable taskW1 = new WriterThreadTask(lock);
Thread t1 = new Thread(taskR1, "Reader1");
Thread t2 = new Thread(taskR2, "Reader2");
Thread t3 = new Thread(taskR3, "Reader3");
Thread t4 = new Thread(taskW1, "Writer1");
t1.start();
t2.start();
t3.start();
/*try{
Thread.sleep(5000);
} catch(InterruptedException e){
e.printStackTrace();
}*/
t4.start();
}

}

---------------------------------------------------------------------------------------------------------------------

Code Observations :

  1. Both notify()/notifyAll() and wait() : works only on the Lock Objects they already acquire. For example : Synchornized(ObjectA) {......// ... // ObjectB.wait() or ObjectB.notify() or ObjectB.notifyAll() ...} then It will throw IllegalMonitorStateException. Therefore care must be taken that lock must be acquired before calling any of above three methods with same lock.Even If you simply write notify() or wait() or notifyAll(), then still it will throw IllegalMonitorStateException because [It suggest lock must be acquired on this Object, again which is not the case].
  2. Reader will only able to receive signals on which same notification is send. If wait is happening on Object which is different from Object on which notification is being send, then Readers will never get the notification and hence they will wait forever.
  3. Readers which are registered before Writer able to send the notification, Only those readers will get it. Because if Writer sends notification first, before reader registered to Object, they will not receive the signals as Signals are already being missed : Missed Signals 
  4. Reader and Writer should acquire Lock on same Object and should invoke wait/notification signals on same Object. If the above code is modified as, instead of using resource for locks and wait and notify, If we use this. What will happen ? Well .. All readers will wait forever, Because readers registered with different Objects of WaitThreadTask and writer notify on WriterThreadTask. Therefore none of the Reader will receive notification signals, as they registered to receive signals on respective WaitThreadTask Object and not on WriterThreadTask Object.


       



Friday, February 20, 2015

NoClassDefinationFound Explained !!

The above Exception occurs when the intended class is not found in the Class Path.
At Compile Time Class : Class was generated from Java Compiler, But Somehow
at Run Time the Dependent Class is not found.

Lets go through one Simple Example :

public class ClassA{
    public static void main(String args[]){
         //Some gibberish Code...
         String text = ClassB.getString();
         System.out.println("Text is :" + text);
    }
}

public class ClassB{

  public static String getString(){
         return "Testing Some Exception";
     }
}


Now Lets assume that the above two Java Source Code are placed in some Folder let say "NoClassDefinationFoundExceptionDemo"

Now open a shell(Assuming Java is already being setup correctly)
1)Go to Folder "NoClassDefinationFoundExceptionDemo"
2)Compile Java Source Files
     javac ClassB
     javac ClassA
3)Both Files are Compiled Sucessfully and generated Class files in the same Folder
   as ClassA.class and ClassB.class
4)Now Since we are overiding ClassPath to current working directory
    therefore we execute the following command
    java -cp . ClassA
    and It worked Successfully and you will see the Output in the screen
5)Now Lets say, You removed  ClassB.class file from Present Directory.
   and now you execute the command again.
   java -cp . ClassA
  Now it will greet you with NoClassDefFoundException. as ClassB which is a dependency
  for ClassA is not found in the classpath(i.e present working directory).

Wednesday, February 18, 2015

Test Pyramid

From the Testing Pyramid Notes :
1)Unit Tests : It should be the building blocks of the tests. Any method, which is doing something non trivial should be the included as a part of unit Test.These tests should be in the same platform where development is done. Developers should take care of writing these tests, as they know what they have developed.
2)UI Tests : UI tests involves lots of setup and execution. Now what happens is let say I have created one Simple Calculator application, and If i test my Sum functionality from UI, it involves lots of Happenings like we will be testing : whether input parameters are properly shown, Button is clicked , output is shown as desired etc.Now here the problem is very simple : We are coupling Service Testing (Sum functionality) with UI, which is really time consuming, plus UI automation is handling multiple responsibilities, apart from UI[presentation testing] we are also testing services {which is really bad}.Plus it will consume whole lot Of effort and Time, because each time input fields are populated then invoke the Service and then check for desired Output. Instead Of it, Just remove Service Testing from UI automation, In service check for all cases where service can work or broke,
 and then For UI just write Automation for Presentation part, to test whether UI is pretty good as per desired and your functionality is working end to end.
3)Service Tests: Where you will tests for services which you are exposing to UI or End Users.

Number Game :

Unit Tests: Since Unit tests are showing whether your code is working in Bits and Pieces.Any method, Class, Module which you find is important and Holding some non Trivial Business Logic, should be the part Of Unit Tests.These Tests should be maximum in Number, because it penetrates all your non important functionality.Quantity of Unit Tests will be much higher since you are covering almost all your Code.So candidates for Unit Testing are much High.
Service Tests : These are the API's or we can say services provided to End User so that they can consume it, Now Candidates for Service Testing are all services which you are exposing to End User to Use Them. So we can say these are not very high but intermediate in number.
UI Tests : Since UI keeps on changing so writing Tests for displays in UI is not much useful and could be Time Consuming, Instead of it UI Testing are good at Testing Navigation Flow and some important Presentation part, So this is the least bothered stuff to Test.


Thats why we have pyramid like structure :
1)On Top is UI[Low Priority, Less Useful, Keeps on Changing no need to put too much effort on That]
2)Then Service Layer [Medium Priority or important priority, since it projects your Business Flow, Again needs to tested thoroughly]
3)Unit Tests[Most important, needs to test your code in Bits and Pieces, plus bugs or any issue can be caught very early, Developers are mostly responsible to write it, to test their own Code].

Wednesday, February 11, 2015

Introduction To Generics

The main Idea behind Generics is to pass Type parameter along with Implementation Itself.
Ex:
   method(Integer x){
//...
   }
   this method only accepts Integer param.
 
   Now let say If we need to create Our Method Generic enough that users of method can pass types also.
   Something like :
   method(T x){
   //...
   }
   Now here T depends on User type.
   Let say :
   User #1
       Integer x = new Integer(5);
  method(x);
User #2
String y = new String("abc");
method(y);

So our method is generic enough to consume any Input Argument Type.
    We will discuss this appraoch more .
    The Other approach is for method to have Object type input parameter.
    method(Object x){
     //...
    }
Now this approach is good to certain extend.
Observations :
a)Since all non primitive types are subclass to Object, so client with non-primitive types can easily use method.
 Integer x = new Integer(5);
 method(x);

 String y = new String("abc");
 method(y);

 Both these will work like a charm.
b)Problem arises when storing elements in collection of any Type just like List<Object>
      Let say we have method :
      methodList(){
   List strList = new ArrayList();
strList.add("abc");
strList.add("def");
//Lets assume list has two elements.
Iterator iter = list.iterator();
while (iter.hasNext()) {
String str = (String)iter.next(); //We need to explicitly Downcast, Since List will return Object element.
System.out.println(str);
}
//Now we need to add one more element, which is Integer.
Integer ints = new Integer(5);
strList.add(ints);
//If we do
String str = (String) strList.get(2);
//This will throw ClassCastException : which means element is getting casted to incompatible Type and throw ClassCastException at RunTime.
}
    So, Upcasting is Implicit, Because all non Primitive types are Subclasses Of Object.which can be done easily.
    But in case of Downcasting, Programmer needs to take care of when to Downcast and Correct Type to Downcast.
3)We can use instance Of while doing downcast, but again its a too much to do that.
 Maintaining each type and its associated if-else condition is too much to do.
    4)Generics helps us to avoid such problems :
      ==> Developer don't need to do the BookKeeping of Downcast, whenever he is retrieving elements from some Global Structure like List, method(Object ) etc.
          Casting problems are traced at Compile Time and not at RunTime, Thanks to Generics from Java5 and above.
      ==>Generics are mainly used to Use Collections easily, without Type Errors etc. Since Collections are used to store ant Type of Elements, and Generics are
    there to provide Type Safety Stuff.
      ==>With Generics you can write Generic Code and User Of your Code provide Specific Type.
         Ex:
            <T> void printList(List<T> list){
for(T ele : list){
System.out.println(ele);
}
}

Now user Of printList, will use it like
List<Integer> ints  = new ArrayList<Integer>();
ints.add(1);ints.add(2);ints.add(3);

Now user Of printList, will use it like
List<String> ints  = new ArrayList<String>();
ints.add("abc");ints.add("xyz");ints.add("def");

So in both cases, we as don't need to handle different Types separately.
and our printList code is generic enough, not to get changed with different Types List from Consumer.

  

Monday, June 24, 2013

Lowest Common Ancestor in Binary Tree(not necessary to be BST)..

Bottom Up Approach:


Explanation :

We traverse the tree only once .. and find out the LCA.Intuition is : 1)First of all we need to traverse the whole tree.Hence any traversing techniue needs to be implemented.[here we are using postorder, because we needs to make decesion based on values calculated.]
    So we added :
        LCA(root.left,p,q);
        LCA(root.right,p,q);
    Now we need certain Inits: if root == null : return null;
            if(root is any of p or q) : return root.
                It means we have reached one of our target node.

2)Decision Block : Since we are returning from bottom to top, after calculations...
    So three cases arises :
        -->Either we found one of our node
        -->Not found one of our node.
        -->Found both nodes
        If we don't find one of our node then  return null;
        But if we found one of our node ... then we need to return that node to its parent.   
        If we found both nodes (then return its root)..Since we are coming from bottom to top, so the first node found having both nodes is our result.


So our code have :

    --> Inits
    --> Traverse Block
    --> Decesion Block. [Why we have written decesion block in last place? Beacuse to make decisions you need to know the values of left subtree and right subtree.]


Ex:

    Let say we have a tree :

        a
        /\                    Let p = f; and q = o       
        b c
        /\ /\
        d e f g
          /\  /
          m n o


For simplicity :
    Lets take a simple tree
        a
        /\
        b c

and p = b, q = c; 
 How our algo proceeds : at A: then goto left subtree and return null||p||q, right subtree and return null||p||q.
    b is returned from leftsubtree and c from right subtree, and root of that will be our LCA


another Example :
        a
        /\            p = d and q = c
        b c
        /\
        d e

    AT A : leftSubtree --> B. then again left subtree... -->D ..return D.
        Now at B... Right sub tree -->At E ...returns null ... since other node is not found.
        So at B value is D.
        Now go to A right subtree .. -->C: return C
        So we have A left Subtree returns D and right subtree returns C..
            SO intuitevely this is the first node, which have p i left subtree and q in right subtree or vice versa.
                          So this is the LCA.

another example :

        a
        /\
        b c                p = m, q= f
        /\ /\
        e f g h
        /\
        m n
   

  We start at A: left subtree -->B -->E -->M since we got M --> so return value to E's left subtree
        E(leftSubtree) = M;
        Now we go to right subtree of E: we found no f ..
        E(right subtree) = null;
        Now non null value is returned to B left subtree value..
        SO B(left subtree) = M.
        Now we calculate B (right subtree)-->We have found 'F'
        So B (right subtree value) = F.
        Hence we return B as LCA   



another example:
        if in previous figure : p=m, q=b
        i.e one node is parent(direct/indirect) of other node ...

        Again as usual, we start at A --> left subtree -->B, now since B is found we return B
            So A(left subtree) = B;[We skipped traversing B childrens]
        Now go to A right subtree -- > we traverse through C and its children an we are unbale to find 'M'--> so it returns null
            So A(right subtree) = M;
        We go to decesion block where we found that non null value should be returned.Hence LCA = B.which is correct.


Time Complexity: Since this algo requires only traversing of tree, So complexity is O(n).
Space Complexity : If tree is balanced then Logn recursive calls and needs to store those on stack.But in worst case it is O(n).




Final Code :

    return : node type
    arguments : root,p,q
    So signature of method : public node LCA(node root, node p, node q)


    Code:

        public node LCA(node root, node p, node q){

        //Inits :
            if(root == null || p == null || q == null)
                return null;
            else if(root == p)
                return p;
            else if(root == q)
                return q;
            else{
                //Traversals
                node L = LCA(root.left,p,q);
                node R = LCA(root.right,p,q)'
                //Decision Code
                if(L != null && R != null)
                    return root; //First node from bottom whose Left and right subtree contains p and q.
                else if(L != null && R== null)
                    return L;
                else if(L == null && R != null)
                    return R;
                else
                    return null;
            }
               
        }
                   
           


Thursday, May 2, 2013

Why Suffix Tree and not Prefix Tree.


To find a pattern we need to find sub string in a text.Since Tries can also find sub string, but there is a catch since they find the sub strings with well defined start and end delimiter.
For ex : Suppose I need to search for a word in a text "bull". Then Trie can do it in O(m) time. But let say we need to find pattern such as "l bid be" now it is a sub-string, but it doesn't have a well defined delimiters starting and ending.
So Basically tries doesn't solve our problem for pattern search.It can find string with well defined boundaries, but not sub string with delimiters.Now this has obvious applications in finding some patterns in Human Gnome.
Like :
"ADCNDJCDNCD CNDJCNDNCDXXCAVDA CDNJCDNCD HBADSCD"
Now if I need to find  "CDNCD HBA" then tries can't do this. Since they stores words and not sub strings.
                                  
Therefore we need some other data structures which can do this.

Firstly our requirement is :
a)We need searching like Tries, means searching of sub string should be done in time proportional to the length of the pattern.
b)Instead of storing individual words in a Trie, we can store either Prefixes or Suffixes of the text(not of the words). Since by this way we can catch the patterns (i.e sub strings) other than individual words.

Now why prefixes or suffixes ?
Since to find sub strings, we must require to search in prefixes or suffixes of text. What it really means is ?
First lets look at Sub string definition.
We can define sub string in two ways:
 -->A prefix of some suffix.
 -->A suffix of some prefix.

For ex :
I have some Text as :
 "abcde h"
 Firstly we must find its prefixes :
  "a"
  "ab"
 "abc"
"abcd"
"abcde"
"abcde h"
Now to find a sub string/pattern let say : "de h"
This sub string must be a suffix of some prefix.
Here in our example it is suffix of last prefix.

Similarly for our other definition of sub string :
We calculate suffixes :
 "h"
" h"
"e h"
"de h"
"cde h"
"bcde h"
"abcde h"
Now to find substring "cde ". It must be  prefix of some suffix.
Here in our case it is prefix of suffix "cde h"

So from this we mean : if we store prefixes or suffixes of the text. We can easily search for a sub string (even without delimiters).

So our next step is :
Create a Trie of prefixes or suffixes of text.

Now our next question is Does Trie of prefixes really do our job?

Logically it seems that we don't require Trie of prefixes.
Since our main concern is the starting delimiter of the sub string.Because if we create a prefix tree, from where we should start our search for pattern.Since our Trie of prefixes always starts with prefixes.Now in order to find intermediate sub string like "cde h", we are not able start the Trie traversal from root of the Trie.
  There are two ways to deal with this problem :
 -->Either create a prefix tree and make some techniques to start traversal from bottom-up.Now this seems to be inappropriate , with some complex traversing from bottom to top,while also to make our Data structure to remain as Trie.
-->Or create a Trie of suffixes.
    Now this will solve our problem, because we will get our starting point from the top of the Trie and match till our sub string length.

So in short we will create a Trie , storing suffixes of entire Text(not individual words).










Monday, June 4, 2012

Shell and Linear Algebra blogs

Some links which are useful to explore for beginners.

Bash Scripting : AboutLinux
Awk One Liner : here
Linear Algebra :here
Unix One Liner :here
Node.js :here

Recovery Mode without grub

 1)After start booting : Ctrl + Alt + F1 --> Recovery Mode
 2)To reboot from terminal : sudo reboot

Tuesday, May 22, 2012

Tomcat - java.lang.OutOfMemoryError: PermGen space problem

When i run tomcat, sometimes i got JVM out of memory. This is due to insufficient tomcat memory allocated.

Solution
Steps :
1)Open catalina.sh in specific tomcat. tomcat/bin/catalina.sh
2)add in JAVA_OPTS value: -server -Xms512m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=256m -XX:+DisableExplicitGC
3)Restart tomcat again

For more you can refer Here

Monday, April 30, 2012

Octave 3.4.3 Installation Ubuntu

1) Sudo apt-get install build-essentials
2)Download octave3.4.3 from Here
3)Untar it, and then install gfortran using sudo apt-get install gfortran
4)apt-get install octave3.2-headers [For building dependencies]
Or
apt-get install libblas-dev libatlas-dev liblapack-dev
5)sudo mv /usr/bin/gfortran    /usr/bin/gfortran.ORG
6)sudo ln -s /usr/bin/gfortran-x.x   /usr/bin/gfortran [create a soft link]
7)Create enviornment variables :
   - export CC=/usr/bin/gcc-x.x
   - export CXX=/usr/bin/g++-x.x
8)./configure in octave folder
9)make [it will take around 30 mins]
10)./run-octave
11)make check
12)sudo make install
13) make
14)make clean
For more Info check here
 
 
 

Wednesday, January 4, 2012

Useful Links for Algorithms and Puzzles

Coding Contest :

1>Code Chef : Initiative of Directi
2>AI challenge : http://aichallenge.org/problem_description.php
3>ML coding contest : http://www.kaggle.com/ [Best]
4>Heritage Health Prize :  Data analysis contest http://www.heritagehealthprize.com/c/hhp


Algorithms :

1>Carrer Cup [Blog /Pdf]
2>Geek for geeks [best]
3>Crack The Interview [Pdf]
4>Freshers world

Puzzles :
1>Gurmeet singh puzzles :Delightful Puzzles
2>Cracking the code : http://shashank7s.blogspot.com/p/stacks.html


Wednesday, December 7, 2011

Git Simple commands

Like other repository managers CVS, git is a also a repository manager. Where we can commit our code, so that it can be updated in repository.

Branches : Whole repository can be managed using two branches : local  and remote branches. Master is the root of all branches where all builds from different branches are committed and are in production version. Local branches are the branches specific for particular project. Remote branches are the branches specific to particular project but not created by us.

Useful git commands :

#git push origin branch-name : push the code to specified branch
#git branch : will show your current branch
#git branch  -a :show list of all branches including remote and local and master branches
#git branch -r : show list of all remote branches.
#git stash : save your local changes into git pull or git svn (so that whenever you git pull --rebase changes will automatically comes in)
#git checkout <branch_name> : To switch to specified branch
#git pull --rebase : pull the updated build from current branch
#git logs : to check the last updates in repository
#gitk : is an Graphical interface to git.
#git status : How many local changes you have made.
#git diff <filename> : shows the differences of changes you have made in that file after last commit/update.
#git branch <branch_name> : create a new branch with <branch_name>

Note for more details refer link

Wednesday, November 30, 2011

String vs String Builder vs String Buffer

String : Immutable
String Builder : Mutable + Thread safe
String Buffer : Mutable + not Thread safe.


Performance wise : string is bad , as for every operation it creates new string object hence immutable. whereas String builder/ String Buffer doesn't create new object very time for string operations.

For ex :

String A = A + "BBC"
then for this a new "A" object will be created. So we are creating those many objects of string class.

When using String Builder/ String Buffer, then operations performed on same object and doesn't create new object of String Buffer/ Builder. Hence mutable.
Therefore performance is good.

##PS : For more refer mutable/immutable

Saturday, October 1, 2011

MySQL Reset Root Password

1> First, stop the database
sudo /etc/init.d/mysql stop

2> Start mysqld_safe [which starts the mysql server in safe option] LIke to restart server when some error occured. For more details click here.
Similarily we are using mysqld_safe for resetting lost/forgot password.

/usr/bin/mysqld_safe --skip-grant-tables &
You have to run above using root (sudo -i)
3> It shows mysql daemon started with databases in /var/lib/mysql
Since server is running with --skip-grant-tables flag, means you can access the database without password
Now write the command :
mysql --user=root mysql
Enter your root password
4> Now since we have to update our password in User table. Therefore 
We provide command :
Update user set Password=PASSWORD("Your New Password") Where User='root';
5> Flush Privileges :
Means clear and reload the priviledges.
So command will be 
flush privileges; More about flush option click here
 
6> Exit Mysql
7>Now You have to stop the Daemon Mysql server
It is a two step process :
  • Firstly bring the Background process to Foreground
    Command used for this is :
    fg
     
     
  • Kill the Foreground process
    Ctrl + c [But sometimes Ctrl +z]
8>Now start the mysql 
sudo /etc/init.d/mysql start
9>Now go to mysql
mysql --user=root --pass=newpassword

Monday, September 5, 2011

apt -get broken packages

1>
sudo apt-get clean
will clear your cache, afterwards try again to install the packages you want.
2>
sudo apt-get install -f

Monday, August 29, 2011

Some important commands in ubuntu

1> To remove an application already installed
sudo apt-get remove <app>

2> To remove application already installed with all config files.
sudo apt-get --purge remove <appn>

3>To list all installed packages
dpkg --list
 dpkg --list less
dpkg --list | grep -i 'app'




Sunday, August 28, 2011

How to make password protected folder in Ubuntu

Steps are :

## Install cfs using sudo apt-get install cfs

## Create the encrypted directory (let say lock)
$ cmkdir lock (It will ask for key i.e passwd for folder should not less than 16 chars)

## So encrypted directory lock is created, now we have to put our secret files, this is done by attaching another directory to lock ( let say directory is clock where we put our personal files)
$ cattach lock clock (again it will ask for key)

## You can look a new directory called /crypt is created  and clock folder is inside it where you put ur personal files. /crypt/clock

## Move ur personal stuff to clock
$ mv personal-files /crypt/clock

## Now, to close ~/lock so no one can get into it, we need to unattach the directory with the cdetach command

## $ cdetach clock 

NOTE : Next time if u want to acess ur personal stuff firstly u should start cfsd so, /etc/init.d/cfsd start and then reattach using cattach and then 
sudo umount -f /crypt/clock


Now u can acess ur files. 
After that u shud deattach using cdetach so that it become protected again.
 

Monday, August 22, 2011

Loading Servlet in Tomcat 5.5.29

1> After installing tomcat 5.5.29, CLASSPATH needs to be set for servlet-api.jar file (which is located in apache-tomcat (in my case it is tomcat0)/common/lib/servlet-api.jar). So CLASSPATH = $CLASSPATH:$CATALINA_HOME/common/lib/servlet-api.jar , where CATALINA_HOME=apache-tomcat folder.

export CLASSPATH

2>Create a directory where you compile all your servlet codes. Let say /usr/Servlet.
Also CLASSPATH needs to be set to this folder also.
i.e CLASSPATH=/usr/Servlet.
export CLASSPATH

3>Create your servlet code (let say example.java). Now compile it to create example.class.
sudo javac -classpath /opt/tomcat0/common/lib/servlet-api.jar HelloWorld.java
where path for servlet jar should be included. i.e

sudo javac -classpath <servlet-jar-path> <example.java>

4>Now goto CATALINA_HOME/webapps/ROOT/WEB-INF
--> Create a classes folder (if not present) and copy example.class file to classes folder.
--> Now in web.xml file add these lines
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>example</servlet-class>
</servlet>


<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/example</url-pattern>
</servlet-mapping>

Above entries to be created inside <web-app>...</web-app> tags available in web.xml file

5> Now stop and start the tomcat .
In my case :
Stop : sudo /etc/init.d/tomcat0 stop
Start : sudo /etc/init.d/tomcat0 start.

6> Goto to browser and type :
http://localhost:8080/example


Thats all